Hello Anderson,
NULL is a special value that causes a lot of trouble in SQL programming.
As a general advice try to avoid it when you can set a default value.
When using ? place holders + EXECUTE USING a NULL value, Genero SQL drivers just pass the parameter as NULL to the database client library, as for any other SQL statement (INSERT, UPDATE, DELETE).
Then database server rules apply regarding NULL comparison...
SQL standard requires NULL comparison with the IS NULL or IS NOT NULL operator.
When using the = equal operator and an operand is NULL, the boolean expression evaluates to FALSE (or NULL, but never to TRUE with NULL=NULL).
Therefore, if you need to search for NULLs and for a specific date in the same query, you will have to adapt the SQL to distinguish "colname = ?" and "colname IS NULL", depending on the NULL state of the parameter...
The problem is that colname = ? requires a USING parameter while colname IS NULL does not.
The number of parameters in the USING clause must match the ? place holders.
So you would have to write different FOREACH USING or EXECUTE USING statements.
The trick here is to add a second parameter to perform the (colname IS NULL) test:
WHERE d=? OR (1 = ? AND d IS NULL)
Try the following:
MAIN
DEFINE vd DATE, nt INT, vk INT
DATABASE test1
--CONNECT TO "test1+driver='dbmpgs'" USER "pgsuser" USING "fourjs"
CREATE TEMP TABLE tt1 ( k INT, d DATE )
INSERT INTO tt1 VALUES ( 1, TODAY )
INSERT INTO tt1 VALUES ( 2, NULL )
DECLARE curs1 CURSOR FROM "SELECT k FROM tt1 WHERE d=? OR (1 = ? AND d IS NULL) ORDER BY k"
DISPLAY "With TODAY in USING parameter:"
LET vd = TODAY
LET nt = 0
FOREACH curs1 USING vd, nt INTO vk
DISPLAY vk
END FOREACH
DISPLAY "With NULL in USING parameter:"
LET vd = NULL
LET nt = 1
FOREACH curs1 USING vd, nt INTO vk
DISPLAY vk
END FOREACH
END MAIN
However, I wonder:
Why would you need to fetch (in the same result set) rows with a specific date, and with nulls in the date?
Don't you want you treat these rows in 2 separate queries?
Seb