all groups > sql server programming > october 2004 >
You're in the

sql server programming

group:

SET NOCOUNT ON OPTION


SET NOCOUNT ON OPTION Leon
10/18/2004 9:35:17 PM
sql server programming:
When and when not to set SET NOCOUNT ON?

I know what it does but not exactly where and when I want to use it.

Re: SET NOCOUNT ON OPTION Adam Machanic
10/18/2004 10:46:19 PM
Personally, I use it at the top of every stored procedure, always. I never
want the rowcounts returned...


[quoted text, click to view]

Re: SET NOCOUNT ON OPTION Leon
10/18/2004 11:00:06 PM
But you always have to set it to NOCOUNT OFF after the action within the
store procedure right?

[quoted text, click to view]

Re: SET NOCOUNT ON OPTION Derrick Leggett
10/18/2004 11:11:29 PM
No, the SET NOCOUNT is specific to the procedure and is set at execution of
the procedure.

[quoted text, click to view]

Re: SET NOCOUNT ON OPTION Aaron [SQL Server MVP]
10/19/2004 9:06:20 AM
[quoted text, click to view]

Can you show an example where the NOCOUNT setting affects @@ROWCOUNT? Like
Adam, I use SET NOCOUNT ON in every single procedure I write, and I have
never found a case where I had to set it OFF at any point in the procedure.

Re: SET NOCOUNT ON OPTION Adam Machanic
10/19/2004 9:13:32 AM

[quoted text, click to view]

Phil,

@@ROWCOUNT and the NOCOUNT option are mutually exclusive:


CREATE TABLE #abc(def INT)

SET NOCOUNT ON

INSERT #abc (def)
VALUES (1)

SELECT @@ROWCOUNT


Re: SET NOCOUNT ON OPTION Philippe [MS]
10/19/2004 10:51:32 AM
You set that value to ON when you do not want to see the row count number
after an insert or update operation.

If your store procedure just SELECT things, do not hesitate to put SET
NOCOUNT ON at the begining of the store procedure.

If you want for example to create a store procedure that make an insert if
the line does not exist and an insert if not, you can do :

UPDATE ...
IF @@ROWCOUNT = 0
BEGIN
INSERT ...
END

Therefore, you need to have the NOCOUNT set to OFF

Phil.

[quoted text, click to view]

Re: SET NOCOUNT ON OPTION Raymond D'Anjou (raydan)
10/19/2004 2:48:49 PM
Setting NOCOUNT ON or OFF has absolutely no effect on @@ROWCOUNT.
Try Adam's little script and you'll see.

[quoted text, click to view]

Re: SET NOCOUNT ON OPTION Aaron [SQL Server MVP]
10/19/2004 3:01:08 PM
[quoted text, click to view]

I'm sorry, but you're mistaken. Adam's repro certainly checks for the
number of rows impacted, and it also certainly works. Can you provide
something concrete that backs up your claim? In the meantime, recall that
SET NOCOUNT ON does not prevent the database from paying attention to what
it's doing to the table(s). It merely suppresses that output from being
displayed to the user or sent back in the stream. This will all become very
clear if you spend thirty seconds in Books Online:

Go / URL / tsqlref.chm::/ts_set-set_3ed0.htm

"The @@ROWCOUNT function is updated even when SET NOCOUNT is ON."

Re: SET NOCOUNT ON OPTION Andrew J. Kelly
10/19/2004 3:01:12 PM
That is just not true. SET NOCOUNT has no effect what so ever on
@@ROWCOUNT.

--
Andrew J. Kelly SQL MVP


[quoted text, click to view]

Re: SET NOCOUNT ON OPTION Philippe [MS]
10/19/2004 8:22:11 PM
It's because you never check the number of rows that are impacted by your
update or insert.

Phil.

[quoted text, click to view]

Re: SET NOCOUNT ON OPTION Philippe [MS]
10/19/2004 9:28:01 PM
Sorry, I write too fast without testing.

It's not valid for @@ROWCOUNT (Cf. SQL Docs << SET NOCOUNT >>).

If you launch a proc / sql from Query analyzer, you see the impact of ON or
OFF in the message table.

Sorry again. :-(

Phil.


[quoted text, click to view]

AddThis Social Bookmark Button