Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Johnph
Posting Yak Master
103 Posts |
Posted - 2012-09-13 : 11:17:38
|
Hi, I am working on an IF ELSE clause and am having some issues. It looks like this:IF EXISTS (SELECT * FROM TABLE WHERE COLUMN = 'CAT') SELECT * FROM TABLE2ELSE EXEC msdb.dbo.sp_start_job 'JOB'; My problem is that SELECT * FROM TABLE WHERE COLUMN = 'CAT' returns NULL and it still executes SELECT * FROM TABLE2 even though it is NULL instead of doing the ELSE clause.Any help on this would be great. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-13 : 11:20:16
|
if you want to look for non null values change select asIF EXISTS (SELECT * FROM TABLE WHERE COLUMN = 'CAT' AND YourCol IS NOT NULL) SELECT * FROM TABLE2ELSE EXEC msdb.dbo.sp_start_job 'JOB'; ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Johnph
Posting Yak Master
103 Posts |
Posted - 2012-09-13 : 11:25:18
|
Is it better to do that or:SELECT 1 FROM TABLE WHERE COLUMN = 'CAT' which is better? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-13 : 12:08:09
|
quote: Originally posted by Johnph Is it better to do that or:SELECT 1 FROM TABLE WHERE COLUMN = 'CAT' which is better?
but this will still return true if the interested column has a null value------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|