Solved

how to  check whether a sql query have result or not using store procedure?

Posted on 2002-07-17
4
169 Views
Last Modified: 2012-05-04
how to  check whether a sql query have result or not using store procedure?

eg. Select * from table
0
Comment
Question by:AStronus
  • 2
4 Comments
 
LVL 2

Expert Comment

by:Paullkha
Comment Utility
@@rowcount
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 200 total points
Comment Utility
That is correct.  Be sure, though, to save the value if you intend to test anyplace after the very next line.  For example, this code won't work as expected:

SELECT * FROM table
SET @date = GETDATE()
IF @@ROWCOUNT > 0

The SET statement resets @@ROWCOUNT (to 1)!  So, if you need the value of @@ROWCOUNT later, do this:

DECLARE @rowcount INT
SELECT * FROM table
SET @rowcount = @@rowcount
SET @date = GETDATE()
IF @rowcount > 0

By the way, the same is true for @@ERROR.

0
 

Author Comment

by:AStronus
Comment Utility
that odd, i have test that before and it didn't work, now it work so weird
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Just be sure to do the save IMMEDIATELY after the SELECT (or INSERT or UPDATE or DELETE -- @@ROWCOUNT works for all) and it should work.  

Note that if you want to save BOTH @@ROWCOUNT and @@ERROR you have to save both in one line using a SELECT, like this:

DECLARE @error INT
DECLARE @rowcount INT

SELECT * FROM anyTable
SELECT @rowcount = @@ROWCOUNT, @error = @@ERROR
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ASP SQL Syntax Duplicate Key 7 64
SQL Inner Join Vs SubQueries 9 23
Report Builder 9 22
SQL Date Retrival 7 21
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

762 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now