Solved

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

Posted on 2002-07-17
4
172 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
ID: 7160386
@@rowcount
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 200 total points
ID: 7160431
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
ID: 7160676
that odd, i have test that before and it didn't work, now it work so weird
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 7160706
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

867 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

25 Experts available now in Live!

Get 1:1 Help Now