Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
VFP9 and null values and NVL()
Message
From
15/08/2006 06:20:00
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01139317
Message ID:
01145516
Views:
36
This message has been marked as a message which has helped to the initial question of the thread.
>>Is there anytime that a field which allows nulls would generate an error on the code:
>>
>>
>>IF myfield.myvalue  && is the value .t.?
>>
>>
>>The field value can be .T., .F., or null
>>
>>It is never generating an error, but I have always had the code:
>>
>>
>>IF (NVL(myfield.myvalue,.F.))
>>
>
>Tracy,
>
>This really depends on how you want NULL to be handled. NULL is NOT .F. or .T. it is "I don't know". So NULL is by definition both .T. and .F. but at the same time NOT .F. and NOT .T. By using NVL you are saying that NULL should be treated as if it were .F.
>
>This is why it is important to NOT allow NULL unless you need that value. NULL is a tricky value to handle correctly. For example;
>
>
>SELECT * FROM TableA WHERE PK NOT IN(SELECT PK FROM TableB) ...
>
>
>What should happen if there is a NULL value in the TableB PKs? Does NULL match any of the TableA PKs? Well as defined NULL means "I don't know what the value is" based on that any NULL from TableB could possibly match any TableA PK because we don't know what the value of the NULL is. At the same time NULL does not match any particular PK value from TableA. By definition NULL does not even match NULL, the answer to NULL = NULL is NULL and that is neither .T. nor .F.
>
>To make the above subquery work the way most people would expect it to work (which is how it works prior to VFP 9 BTW) you need to ...
>
>
>SELECT * FROM TableA WHERE PK NOT IN(SELECT PK FROM TableB WHERE NOT ISNULL(PK)) ...
>
>
>to eliminate the NULLs from the result of the subquery.

Jim, I don't agree

The IN clause is a "OR" comparison, and then the first work in expected manner.

- Before VFP9 a bug return 2,NULL, on VFP9 the second SELECT return 2
and then it is different.

code explanation:
SET NULL ON
CLOSE TABLES ALL

CREATE CURSOR TableA (pk i)

INSERT INTO TableA VALUES (1)
INSERT INTO TableA VALUES (2)
INSERT INTO TableA VALUES (null)

CREATE CURSOR TableB (pk i)
INSERT INTO TableB VALUES (1)
INSERT INTO TableB VALUES (null)

* vfp8sp1 : 2,null	  (IS A BUG)
* vfp9sp1 : none
SELECT * FROM TableA WHERE NOT PK IN (SELECT PK FROM TableB)

* equal direct form
* vfp8sp1 : none 
* vfp9sp1 : none      (IS A SERIOUS BUG)
SELECT * FROM TableA WHERE NOT PK IN (1,null)

* PROOF: REWRITE IT IN EXPLICIT FORM 
* vfp8sp1 : none
* vfp9sp1 : none
SELECT * FROM TableA WHERE (NOT (PK=1 OR PK=NULL))=.T.

* remove null in conditional set
* vfp8sp1 : 2,null	  (IS A BUG)
* vfp9sp1 : 2
SELECT * FROM TableA WHERE NOT PK IN (SELECT PK FROM TableB WHERE PK IS NOT NULL)

* equal direct form
* vfp8sp1 : 2
* vfp9sp1 : 2
SELECT * FROM TableA WHERE NOT PK IN (1)

* PROOF: REWRITE IT IN EXPLICIT FORM 
* vfp8sp1 : 2
* vfp9sp1 : 2
SELECT * FROM TableA WHERE (NOT (PK=1))=.T.
Previous
Reply
Map
View

Click here to load this message in the networking platform