Search code examples
sqlselectexceptdb2-zos

DB2 SELECT EXCEPT with WHERE clause


I'm trying to compare two tables in a DB2 database in z/OS using SPUFI to submit SQL queries.

I'm doing this by using EXCEPT to see the difference between two SELECT queries.

I need to filter the SELECT statement from the first query with a WHERE clause.

SELECT KEY_FIELD_1,LOOKUP_FIELD_1  
FROM TABLE_1  
WHERE FILTER_FIELD = '1'  
EXCEPT  
SELECT KEY FIELD_2,LOOKUP_FIELD_2  
FROM TABLE_2

I got results back, but it also returned an error -199 Is this because the WHERE clause is not present in the second SELECT statement?

ERROR: ILLEGAL USE OF KEYWORD EXCEPT.  
TOKEN <ERR_STMT> <WNG_STMT> GET SQL  
SAVEPOINT HOLD FREE ASSOCIATE WAS EXPECTED

Solution

  • Try introducing parentheses e.g.

    ( SELECT KEY_FIELD_1,LOOKUP_FIELD_1  
    FROM TABLE_1  
    WHERE FILTER_FIELD = '1' )  
    EXCEPT  
    ( SELECT KEY FIELD_2,LOOKUP_FIELD_2  
    FROM TABLE_2 )