?
Solved

How to exit stored procedure when records=0

Posted on 2012-08-23
6
Medium Priority
?
582 Views
Last Modified: 2012-08-24
Hi!

How do i exit a stored procedure in sql. When records in a table is 0
0
Comment
Question by:team2005
6 Comments
 
LVL 18

Accepted Solution

by:
Cluskitt earned 1400 total points
ID: 38324266
You can do:
DECLARE NumRec int
SET NumRec=(SELECT COUNT(*) FROM MyTable)
IF NumRec>0
BEGIN
  --rest of code here
END
0
 
LVL 9

Assisted Solution

by:mimran18
mimran18 earned 600 total points
ID: 38324361
Drop Procedure sp_test
GO
Create Procedure sp_test
As

IF (Select Count(*) from [tablename])>0
Begin
      Print 'No record found'
      return
End


GO

sp_test
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 38324375
Actually, if select count>0 then you should print 'Found records'. But it's pretty much the same as my solution, only I used a variable to store the count number.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 9

Expert Comment

by:mimran18
ID: 38324420
You are right @cluskit. i did not see your solution and posted it.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38324616
There many ways to do this.  Here is one way:

IF NOT EXISTS (SELECT 1 FROM YourTableName)
     RETURN

-- Rest of you code here

Unfortunately, without knowing more details it is difficult to be more precise.
0
 
LVL 2

Author Closing Comment

by:team2005
ID: 38328215
thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question