If exists or exists?

Can I test two EXISTS conditions in the same IF SQL statement? I have tried the following.

 IF EXIST (SELECT * FROM tblOne WHERE field1 = @parm1 AND field2 = @parm2) OR EXIST (SELECT * FROM tblTwo WHERE field1 = @parm5 AND field2 = @parm3) 

I tried playing with the addition of extra IF and brackets there, but to no avail.

Can you help me with the correct syntax?

+6
source share
3 answers

If SQL Server

 IF EXISTS (SELECT * FROM tblOne WHERE field1 = @parm1 AND field2 = @parm2) OR EXISTS (SELECT * FROM tblTwo WHERE field1 = @parm5 AND field2 = @parm3) PRINT 'YES' 

Great, note that only one thing has changed: EXISTS not EXIST . The plan for this is likely to be UNION ALL , that short circuits, if first checked, are true.

+13
source

You missed S at the end of EXIST

EXIST S , not EXIST

+5
source

You can also write the IN instruction

 IF EXISTS (SELECT * FROM tblOne WHERE field1 = @parm1 AND field2 IN (@parm2,@parm3) 
-1
source

All Articles