Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing info from dynamic TSQL statements

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,

I am writing a stored procedure which needs to execute some dynamic TSQL SELECT statement and find out how many rows were returned.

It is written in a similar fashion to the following:

------------------------------------------------------------
...
DECLARE @sql
DECLARE @rowsfound
SELECT @sql = 'SELECT ... (etc)'

EXEC (@sql)
SELECT @rowsfound = @@rowcount
...
------------------------------------------------------------

N.B. In the situation where I have used code similar to the above, the number of rows returned is always 1 or 0.

The code works fine if I execute it from the Query Analyser. However, if I execute it from a call in my ASP program (which is ultimately where I want to use it from), then @@rowcount always seems to return 0 regardless of whether the correct answer is 1 or 0. The command used to start the stored procedure is exactly the same in both cases.

Does anyone know if this is a known "feature" in SQL or does anyone have an alternative way of obtaining the number of rows returned by the dynamic SELECT call?

Thanks in advance,
Simon.

 
Hi Simon,

I am afraid that you have found one of the slight perculiarities of dynamic SQL. It only appears to stored 0 or 1 to @@rowcount, regardless of the actual number of rows that it has processed. This makes it very difficult if you actually want to report on the number of rows it has processed.

I have not managed to find a way round this problem yet....

Also, it appears to affect all DML (Inserts, Updates etc) as well as selects

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top