Solved

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

Posted on 2002-07-17
4
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Expert Comment

by:Paullkha
ID: 7160386
@@rowcount
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher 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:Scott Pletcher
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

691 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