Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Retrieving values returned by a dynamic SQL query in a variable

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-06-02 : 08:06:55
savitha nair writes "I have a dynamic query, in my stored procedure that returns the total no. of records, matching a given criteria. Based on the count of records returned from this dynamic query, the rest of the stored procedure is supposed to get executed.
Though the dynamic SQL works fine and returns the correct no. of records, Iam not able to use the result to proceed with the stored procedure.
How do I retrieve the result of my SQL dynamic query in a variable?"

SamC
White Water Yakist

3467 Posts

Posted - 2003-06-02 : 08:30:46
Depends on how you are returning the result.

Look up articles on "output variables" see: [url]http://sqlteam.com/item.asp?ItemID=2644[/url]

on SQLTEAM's homepage.


But if you are returning the rows in a recordset, then it's another matter. Use @@ROWCOUNT to get the number of records in this case.


On the other hand, if you are returning COUNT(*) as a column in a single row recordset, then you have a problem.

The following solution doesn't return a recordset, just the count

DECLARE @Mycount INT

SELECT @Mycount = COUNT(*) FROM MyTable WHERE my-condition



Sam

Go to Top of Page
   

- Advertisement -