Note that there are some explanatory texts on larger screens.

plurals
  1. POSET NOCOUNT ON usage
    text
    copied!<p>Inspired by <a href="https://stackoverflow.com/questions/1483383/is-this-stored-procedure-thread-safe-or-whatever-the-equiv-is-on-sql-server">this question</a> where there are differing views on SET NOCOUNT...</p> <blockquote> <p>Should we use SET NOCOUNT ON for SQL Server? If not, why not?</p> </blockquote> <p><strong>What it does</strong> Edit 6, on 22 Jul 2011</p> <p>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)</p> <p>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)</p> <p><strong>Background:</strong></p> <p>General accepted best practice (I thought until this question) is to use <code>SET NOCOUNT ON</code> in triggers and stored procedures in SQL Server. We use it everywhere and a quick google shows plenty of SQL Server MVPs agreeing too.</p> <p>MSDN says this can break a <a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx" rel="noreferrer">.net SQLDataAdapter</a>.</p> <p>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:</p> <ul> <li>IF EXISTS to avoid duplicates (no rows affected message) <em>Note: use with caution</em></li> <li>WHERE NOT EXISTS (less rows then expected</li> <li>Filter out trivial updates (eg no data actually changes)</li> <li>Do any table access before (such as logging)</li> <li>Hide complexity or denormlisation</li> <li>etc</li> </ul> <p>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).</p> <p>It's possible I'm missing something (feel free to point out the obvious), but what do you folks out there think?</p> <p>Note: it's been years since I saw this error because I don't use SQLDataAdapter nowadays.</p> <p><strong>Edits after comments and questions:</strong></p> <p>Edit: More thoughts...</p> <p>We have multiple clients: one may use a C# SQLDataAdaptor, another may use nHibernate from Java. These can be affected in different ways with <code>SET NOCOUNT ON</code>.</p> <p>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.</p> <p>Edit 2: a <a href="https://stackoverflow.com/questions/1354362">trigger breaking nHibernate question</a>, where <code>SET NOCOUNT ON</code> can not be set</p> <p>(and no, it's not a duplicate of <a href="https://stackoverflow.com/questions/995589/set-nocount-off-or-return-rowcount">this</a>)</p> <p>Edit 3: Yet more info, thanks to my MVP colleague</p> <ul> <li><a href="http://support.microsoft.com/?scid=kb%3Ben-us%3B240882&amp;x=4&amp;y=9" rel="noreferrer">KB 240882</a>, issue causing disconnects on SQL 2000 and earlier</li> <li><a href="http://sqlmag.com/sql-server-2000/seeing-believing" rel="noreferrer">Demo of performance gain</a> </li> </ul> <p>Edit 4: 13 May 2011</p> <p><a href="https://stackoverflow.com/q/5880413/27535">Breaks Linq 2 SQL too when not specified?</a></p> <p>Edit 5: 14 Jun 2011</p> <p>Breaks JPA, stored proc with table variables: <a href="https://stackoverflow.com/q/6344631/27535">Does JPA 2.0 support SQL Server table variables?</a></p> <p>Edit 6: 15 Aug 2011</p> <p>The SSMS "Edit rows" data grid requires SET NOCOUNT ON: <a href="https://stackoverflow.com/q/7067329/27535">Update trigger with GROUP BY</a></p> <p>Edit 7: 07 Mar 2013</p> <p>More in depth details from @RemusRusanu:<br> <a href="https://stackoverflow.com/questions/1915405/does-set-nocount-on-really-make-that-much-of-a-performance-difference/1918085#1918085">Does SET NOCOUNT ON really make that much of a performance difference</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload