Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
T-SQL (SS2K5)
»
Conditional Execution of A Procedure
Conditional Execution of A Procedure
Rate Topic
Display Mode
Topic Options
Author
Message
david.chakraborty
david.chakraborty
Posted Thursday, April 16, 2009 1:07 PM
Forum Newbie
Group: General Forum Members
Last Login: Thursday, July 09, 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
JacekO
JacekO
Posted Thursday, April 16, 2009 1:17 PM
SSC-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
david.chakraborty
david.chakraborty
Posted Thursday, April 16, 2009 1:25 PM
Forum Newbie
Group: General Forum Members
Last Login: Thursday, July 09, 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
JacekO
JacekO
Posted Thursday, April 16, 2009 1:32 PM
SSC-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
david.chakraborty
david.chakraborty
Posted Thursday, April 16, 2009 1:32 PM
Forum Newbie
Group: General Forum Members
Last Login: Thursday, July 09, 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
david.chakraborty
david.chakraborty
Posted Thursday, April 16, 2009 1:33 PM
Forum Newbie
Group: General Forum Members
Last Login: Thursday, July 09, 2009 3:11 PM
Points: 7,
Visits: 10
Thanks JacekO
Post #698840
RBarryYoung
RBarryYoung
Posted Thursday, April 16, 2009 3:03 PM
SSCrazy Eights
Group: General Forum Members
Last Login: Saturday, May 04, 2013 11:13 AM
Points: 9,855,
Visits: 9,374
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
Jeffrey Williams 3188
Jeffrey Williams 3188
Posted Thursday, April 16, 2009 3:20 PM
SSCarpal Tunnel
Group: General Forum Members
Last Login: Friday, May 17, 2013 11:21 AM
Points: 4,317,
Visits: 9,216
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
david.chakraborty
david.chakraborty
Posted Thursday, April 16, 2009 3:25 PM
Forum Newbie
Group: General Forum Members
Last Login: Thursday, July 09, 2009 3:11 PM
Points: 7,
Visits: 10
Thanks Everyone! I have already made the change.
Post #698946
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.