Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Conditional Execution of A Procedure Expand / Collapse
Author
Message
Posted Thursday, April 16, 2009 1:07 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 9, 2009 3:11 PM
Points: 7, Visits: 10
I wanted to run a stored proc based on a the following condition

If

Select Count(*) from Table>0
Then
Exec StoredProc
Else Don't Exec

How do I do this? Please help!
Post #698795
Posted Thursday, April 16, 2009 1:17 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, March 29, 2011 2:59 PM
Points: 473, Visits: 606
IF EXISTS (SELECT * FROM table)
EXEC StoredProc


---------------------------------------------
Nothing is impossible.
It is just a matter of time and money.
Post #698814
Posted Thursday, April 16, 2009 1:25 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 9, 2009 3:11 PM
Points: 7, Visits: 10
Let me clarify

If the count returns 0 -- I don't run the SP

If count>0 I run the sp

Table will always be there. SP runs when table has data. Doesn't run when table has no data.
Post #698825
Posted Thursday, April 16, 2009 1:32 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, March 29, 2011 2:59 PM
Points: 473, Visits: 606
Yes, that should work. I assume you call the SP from within another SP.

---------------------------------------------
Nothing is impossible.
It is just a matter of time and money.
Post #698834
Posted Thursday, April 16, 2009 1:32 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 9, 2009 3:11 PM
Points: 7, Visits: 10
I added

(Select Count(*) from Table)>0 in the where clause and it seemed to have worked
Post #698836
Posted Thursday, April 16, 2009 1:33 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 9, 2009 3:11 PM
Points: 7, Visits: 10
Thanks JacekO
Post #698840
Posted Thursday, April 16, 2009 3:03 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,902, Visits: 9,480
david.chakraborty (4/16/2009)
I added

(Select Count(*) from Table)>0 in the where clause and it seemed to have worked
The EXISTS function is faster.


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #698926
Posted Thursday, April 16, 2009 3:20 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 1:03 PM
Points: 4,388, Visits: 9,510
The EXISTS function returns true if there is at least one row returned from the query. Using EXISTS will be faster than a count.

Jeffrey Williams
Problems are opportunites brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs
Post #698940
Posted Thursday, April 16, 2009 3:25 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 9, 2009 3:11 PM
Points: 7, Visits: 10
Thanks Everyone! I have already made the change.
Post #698946
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse