I have a stored procedure that executes multiple statements (including calls
to other procedures). I'd like to have @@ROWCOUNT to be set to a certain
number just before the procedure exists. Basically I'd like to preserve the
@@ROWCOUNT from one of the statements only. Is there a way to do that in
T-SQL?
Thanks,
Bogdan
Hi, you can pass a parameter to your stored procedure for the number
of rows you want to return, per instance @MaxRow.
Then in your stored procedure, you can use the following statement.
-- Set maximum rows returned. If set to 0, all rows are returned.
SET ROWCOUNT @MaxRow;
-- Do whatever you want to do
-- Turn off the row limiter.
SET ROWCOUNT 0;
Does it help ?
Thanks for the reply. Unfortunately this does not help. I do not need to
limit number of rows returned from a query. What I'd like to do is to save
@@ROWCOUNT and then restore it at the end of the proc.
But, as Dan explained above, this is not possible. The problem is that I
can modify the proc but I cannot modify applications that call it. I'm
afraid that some of the apps might rely on @@ROWCOUNT. One example would be
asp.net where AffectedRows reflect @@ROWCOUNT, I think.
Thanks,
Bogdan
ML
---
Matija Lah, SQL Server MVP
http://milambda.blogspot.com/
The ideal solution would be:
INSERT INTO table
-- Save @@ROWCOUNT
DECLARE @RowCountSav int;
SET @RowCountSav = @@ROWCOUNT; -- this resets @@ROWCOUNT
-- If successful, execute the new code
IF @RowCountSav = 1
BEGIN
[...]
END
-- Restore the initial @@ROWCOUNT
SET @ROWCOUNT = @RowCountSav; -- this cannot be done, of course
Bogdan
"ML" <M...@discussions.microsoft.com> wrote in message
news:466953DB-46B4-4709...@microsoft.com...
SELECT n INTO #Junk
FROM Numbers
WHERE n <= @RowCountSav;
That in effect will set the final @@ROWCOUNT to your saved value.
HTH,
Plamen Ratchev
http://www.SQLStudio.com
"Plamen Ratchev" <Pla...@SQLStudio.com> wrote in message
news:E6660ACC-924B-420F...@microsoft.com...