Thursday, March 22, 2012

Clarification on the ROWCOUNT statement

Hi All,
I just wanted to get some clarification on the ROWCOUNT statement in T-SQL.
I'm presently using it to limit the number of rows returned in SELECT based
SPs.
However I'm just curious as the effect of this keyword on the global scope
of SQL operations.
Does setting ROWCOUNT to 25 in one stored procedure effect other SPs running
?
Or is it only specific to the SP which set it?
Also...if it does only effect the code in the specific SP which set the
ROWCOUNT why is it recommended/required to set it back to zero when you are
done?
I have tested this and is "seems" to have no effect on other code running,
but I just want to make sure before I create considerable headaches in my
application.
Thanks for any feedback.
John RossitterJohn,
You can just use the TOP clause with an ORDER BY clause to ommit the need of
setting ROWCOUNT.
From SQL BOL:
It is recommended that DELETE, INSERT, and UPDATE statements currently using
SET ROWCOUNT be rewritten to use the TOP syntax. For more information, see
DELETE, INSERT, or UPDATE.
The setting of the SET ROWCOUNT option is ignored for INSERT, UPDATE, and
DELETE statements against remote tables and local and remote partitioned
views.
HTH
Jerry
"John Rossitter" <JohnRossitter@.discussions.microsoft.com> wrote in message
news:2095A9F1-B48A-40A3-8D40-A19A904B9BDD@.microsoft.com...
> Hi All,
> I just wanted to get some clarification on the ROWCOUNT statement in
> T-SQL.
> I'm presently using it to limit the number of rows returned in SELECT
> based
> SPs.
> However I'm just curious as the effect of this keyword on the global scope
> of SQL operations.
> Does setting ROWCOUNT to 25 in one stored procedure effect other SPs
> running?
> Or is it only specific to the SP which set it?
> Also...if it does only effect the code in the specific SP which set the
> ROWCOUNT why is it recommended/required to set it back to zero when you
> are
> done?
> I have tested this and is "seems" to have no effect on other code running,
> but I just want to make sure before I create considerable headaches in my
> application.
> Thanks for any feedback.
> John Rossitter
>|||The ROWCOUNT setting does not bleed to other connections and it is also loca
l to inside the
procedure. I still always type a comment to reset it to 0 the very same mome
nt as I type the setting
to non-zero. Just in case someone else add some code to the procedure later.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"John Rossitter" <JohnRossitter@.discussions.microsoft.com> wrote in message
news:2095A9F1-B48A-40A3-8D40-A19A904B9BDD@.microsoft.com...
> Hi All,
> I just wanted to get some clarification on the ROWCOUNT statement in T-SQL
.
> I'm presently using it to limit the number of rows returned in SELECT base
d
> SPs.
> However I'm just curious as the effect of this keyword on the global scope
> of SQL operations.
> Does setting ROWCOUNT to 25 in one stored procedure effect other SPs runni
ng?
> Or is it only specific to the SP which set it?
> Also...if it does only effect the code in the specific SP which set the
> ROWCOUNT why is it recommended/required to set it back to zero when you ar
e
> done?
> I have tested this and is "seems" to have no effect on other code running,
> but I just want to make sure before I create considerable headaches in my
> application.
> Thanks for any feedback.
> John Rossitter
>|||Hi Jerry,
I can't use TOP because I need a dynamic number of rows selected.
Which is why I'm using ROWCOUNT directive instead.
To clarify what I have done, is in one of my tables set a MaxRows threshold.
Now when any of my SELECT based SPs execute the code looks something like
this:
====================================
DECLARE @.TOP int;
SET @.TOP = 1000
SELECT @.TOP = Max_Record_Count FROM Control_Table
SET ROWCOUNT @.TOP
SELECT ....
SET ROWCOUNT 0
=====================================
This way I can control the overall performance of all of my queries.
I just want to make sure that the ROWCOUNT setting does not have an impact
on any other code running in SQL.
For example lets say that the above SP was running, and then another started
in the middle of it's execution. Should I expect to see the ROWCOUNT clause
effect the 2nd query, or does it only effect the code which it's currently
executing.
I hope that helps clarify my question.
Thanks,
John Rossitter
"Jerry Spivey" wrote:

> John,
> You can just use the TOP clause with an ORDER BY clause to ommit the need
of
> setting ROWCOUNT.
> From SQL BOL:
> It is recommended that DELETE, INSERT, and UPDATE statements currently usi
ng
> SET ROWCOUNT be rewritten to use the TOP syntax. For more information, see
> DELETE, INSERT, or UPDATE.
> The setting of the SET ROWCOUNT option is ignored for INSERT, UPDATE, and
> DELETE statements against remote tables and local and remote partitioned
> views.
> HTH
> Jerry
> "John Rossitter" <JohnRossitter@.discussions.microsoft.com> wrote in messag
e
> news:2095A9F1-B48A-40A3-8D40-A19A904B9BDD@.microsoft.com...
>
>|||Ok. I'm not aware of any issues. My testing confirms the same results - no
side affects. However that being said, I would recommend appending a SET
ROWCOUNT 0 to your code for completeness.
HTH
Jerry
"John Rossitter" <JohnRossitter@.discussions.microsoft.com> wrote in message
news:9596F2C7-649D-4EDD-98B3-A7C321FD4038@.microsoft.com...
> Hi Jerry,
> I can't use TOP because I need a dynamic number of rows selected.
> Which is why I'm using ROWCOUNT directive instead.
> To clarify what I have done, is in one of my tables set a MaxRows
> threshold.
> Now when any of my SELECT based SPs execute the code looks something like
> this:
> ====================================
> DECLARE @.TOP int;
> SET @.TOP = 1000
> SELECT @.TOP = Max_Record_Count FROM Control_Table
> SET ROWCOUNT @.TOP
> SELECT ....
> SET ROWCOUNT 0
> =====================================
> This way I can control the overall performance of all of my queries.
> I just want to make sure that the ROWCOUNT setting does not have an impact
> on any other code running in SQL.
> For example lets say that the above SP was running, and then another
> started
> in the middle of it's execution. Should I expect to see the ROWCOUNT
> clause
> effect the 2nd query, or does it only effect the code which it's currently
> executing.
> I hope that helps clarify my question.
> Thanks,
> John Rossitter
>
> "Jerry Spivey" wrote:
>|||Hi
You can use top in dynamic queries like this
DECLARE @.TOP int;
DECLARE @.VAR varchar(200)
SET @.TOP = 1000
SELECT @.TOP = Max_Record_Count FROM Control_Table
select @.VAR = 'SELECT TOP' + CAST(@.TOP as varchar(10) + ' from your table
name...'
exec(@.VAR)
--
Regards
R.D
--Knowledge gets doubled when shared
"John Rossitter" wrote:
> Hi Jerry,
> I can't use TOP because I need a dynamic number of rows selected.
> Which is why I'm using ROWCOUNT directive instead.
> To clarify what I have done, is in one of my tables set a MaxRows threshol
d.
> Now when any of my SELECT based SPs execute the code looks something like
> this:
> ====================================
> DECLARE @.TOP int;
> SET @.TOP = 1000
> SELECT @.TOP = Max_Record_Count FROM Control_Table
> SET ROWCOUNT @.TOP
> SELECT ....
> SET ROWCOUNT 0
> =====================================
> This way I can control the overall performance of all of my queries.
> I just want to make sure that the ROWCOUNT setting does not have an impact
> on any other code running in SQL.
> For example lets say that the above SP was running, and then another start
ed
> in the middle of it's execution. Should I expect to see the ROWCOUNT claus
e
> effect the 2nd query, or does it only effect the code which it's currently
> executing.
> I hope that helps clarify my question.
> Thanks,
> John Rossitter
>
> "Jerry Spivey" wrote:
>

No comments:

Post a Comment