See this ...
I am using Borland Local SQL which is a subset of ANSI92 SQL.
Looks simple yeah. Joins two tables. Stockcode is unique in both tables so the join is not "one to many" (I would use Group By if it was).
If all the stock codes in table A exist in table B then the result is as expected.
Lets say not all the stock code exists in table B, because perhaps table B starts empty and builds over time. I have included a left outer join to ensure that a record still gets returned for every record in table A.
Here's where the stinker comes in ... If a record doesn't exist in Table B, B.Allocated will return null (not zero) which should be fine but it ISN'T because the Where clause just simply decides to exlude the entire record because of the null value, it doesn't treat it as zero. ARG!
I tried doing 0+B.Allocated but that still fails. I tried Cast(0+B.Allocated as Integer) but this gives totally duff results as Cast seems to use the last valid value read for B.Allocated instead of the correct one which is null. I'm sure this is a Borland Local SQL bug. I know it's doing something dumb as if you change the minus sign to a plus sign you get all the records that the other sign missed out which is ridiculous as +0 and -0 are the same.
Anyway I can fix it by boringly building a Breakdown table that has one record for everystock item and then using that so we get 0 instead of null. Or I can change the where line to this:
Note how I repeat the equasion in the brackets but leaving out B.Allocted as part of it. The problem with this technique is the SQL I have posted is massively simplified from the one I'm working on which uses B.Allocated about 6 times in different places (no it can't be optimised) and thus I'm worried that adding in a null test 6 timescould be slower than building a temp Breakdown table with a record for every stock record beforehand. I guess I won't know unless I test. Plus of course it makes the SQL even more messy and complicated.
I just wondered if any of you SQL heads have ever seen this behaviour of null causing Where to totally exclude a line even with a left outer join instead of just treating it like 0 or if this is perculiar to Borland's Local SQL. Actually I'm pretty sure I found this stinker out a long time ago and had to do a crappy workaround then too.
Thanks in advance if any of you understand what I'm on about and feel like commenting!
select * from "e:\server\data\AK_Stock" A left outer join "e:\server\data\AK_Breakdown" B on A.StockCode = B.StockCode where A.StockFree + B.Allocated > 0
I am using Borland Local SQL which is a subset of ANSI92 SQL.
Looks simple yeah. Joins two tables. Stockcode is unique in both tables so the join is not "one to many" (I would use Group By if it was).
If all the stock codes in table A exist in table B then the result is as expected.
Lets say not all the stock code exists in table B, because perhaps table B starts empty and builds over time. I have included a left outer join to ensure that a record still gets returned for every record in table A.
Here's where the stinker comes in ... If a record doesn't exist in Table B, B.Allocated will return null (not zero) which should be fine but it ISN'T because the Where clause just simply decides to exlude the entire record because of the null value, it doesn't treat it as zero. ARG!
I tried doing 0+B.Allocated but that still fails. I tried Cast(0+B.Allocated as Integer) but this gives totally duff results as Cast seems to use the last valid value read for B.Allocated instead of the correct one which is null. I'm sure this is a Borland Local SQL bug. I know it's doing something dumb as if you change the minus sign to a plus sign you get all the records that the other sign missed out which is ridiculous as +0 and -0 are the same.
Anyway I can fix it by boringly building a Breakdown table that has one record for everystock item and then using that so we get 0 instead of null. Or I can change the where line to this:
where A.StockFree - B.Allocated > 0 or (B.Allocated is null and A.StockFree > 0)
Note how I repeat the equasion in the brackets but leaving out B.Allocted as part of it. The problem with this technique is the SQL I have posted is massively simplified from the one I'm working on which uses B.Allocated about 6 times in different places (no it can't be optimised) and thus I'm worried that adding in a null test 6 timescould be slower than building a temp Breakdown table with a record for every stock record beforehand. I guess I won't know unless I test. Plus of course it makes the SQL even more messy and complicated.
I just wondered if any of you SQL heads have ever seen this behaviour of null causing Where to totally exclude a line even with a left outer join instead of just treating it like 0 or if this is perculiar to Borland's Local SQL. Actually I'm pretty sure I found this stinker out a long time ago and had to do a crappy workaround then too.
Thanks in advance if any of you understand what I'm on about and feel like commenting!