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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Executing SP from else clause

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 TABLE2

ELSE

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 as

IF EXISTS (SELECT * FROM TABLE WHERE COLUMN = 'CAT' AND YourCol IS NOT NULL)

SELECT * FROM TABLE2

ELSE

EXEC msdb.dbo.sp_start_job 'JOB';



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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?
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -