Inspired by this question where there are differing views on SET NOCOUNT...
Should we use SET NOCOUNT ON for SQL Server? If not, why not?
What it does Edit 6, on 22 Jul 2011
It suppresses the "xx rows affected" message after any DML. This is a resultset and when sent, the client must process it. It's tiny, but measurable (see answers below)
For triggers etc, the client will receive multiple "xx rows affected" and this causes all manner of errors for some ORMs, MS Access, JPA etc (see edits below)
Background:
General accepted best practice (I thought until this question) is to use SET NOCOUNT ON
in triggers and stored procedures in SQL Server. We use it everywhere and a quick google shows plenty of SQL Server MVPs agreeing too.
MSDN says this can break a .net SQLDataAdapter.
Now, this means to me that the SQLDataAdapter is limited to utterly simply CRUD processing because it expects the "n rows affected" message to match. So, I can't use:
In the question marc_s (who knows his SQL stuff) says do not use it. This differs to what I think (and I regard myself as somewhat competent at SQL too).
It's possible I'm missing something (feel free to point out the obvious), but what do you folks out there think?
Note: it's been years since I saw this error because I don't use SQLDataAdapter nowadays.
Edits after comments and questions:
Edit: More thoughts...
We have multiple clients: one may use a C# SQLDataAdaptor, another may use nHibernate from Java. These can be affected in different ways with SET NOCOUNT ON
.
If you regard stored procs as methods, then it's bad form (anti-pattern) to assume some internal processing works a certain way for your own purposes.
Edit 2: a trigger breaking nHibernate question, where SET NOCOUNT ON
can not be set
(and no, it's not a duplicate of this)
Edit 3: Yet more info, thanks to my MVP colleague
Edit 4: 13 May 2011
Breaks Linq 2 SQL too when not specified?
Edit 5: 14 Jun 2011
Breaks JPA, stored proc with table variables: Does JPA 2.0 support SQL Server table variables?
Edit 6: 15 Aug 2011
The SSMS "Edit rows" data grid requires SET NOCOUNT ON: Update trigger with GROUP BY
Edit 7: 07 Mar 2013
More in depth details from @RemusRusanu:
Does SET NOCOUNT ON really make that much of a performance difference
Ok now I've done my research, here is the deal:
In TDS protocol, SET NOCOUNT ON
only saves 9-bytes per query while the text "SET NOCOUNT ON" itself is a whopping 14 bytes. I used to think that 123 row(s) affected
was returned from server in plain text in a separate network packet but that's not the case. It's in fact a small structure called DONE_IN_PROC
embedded in the response. It's not a separate network packet so no roundtrips are wasted.
I think you can stick to default counting behavior almost always without worrying about the performance. There are some cases though, where calculating the number of rows beforehand would impact the performance, such as a forward-only cursor. In that case NOCOUNT might be a necessity. Other than that, there is absolutely no need to follow "use NOCOUNT wherever possible" motto.
Here is a very detailed analysis about insignificance of SET NOCOUNT
setting: http://daleburnett.com/2014/01/everything-ever-wanted-know-set-nocount/