System Views, Stored Procedures and SET NOCOUNT
Problems with data providers and SET NOCOUNT
Its standard good practise to always use SET NOCOUNT ON in your stored procedures to prevent system messages about the number of rows affected by DML statements from being returned. However as well as being good practise I have found that it can actually cause issues in certain cases depending on the data access provider used to connect to your SQL database from your front end application. If you have read my article about migrating from 32 bit to 64 bit applications you will see that I mention certain issues there. I have experienced problems when moving from MDAC to SQLOLEDB when stored procedures contain multiple DML statements and SET NOCOUNT has not been set as it seems these system messages are being returned as a recordset and whereas MDAC will ignore them SQLOLEDB doesn't. Therefore you may experience errors such as "Operation not allowed when the object is closed" or "item or ordinal does not exist in collection" because these system messages are being returned or accessed before your intended recordset is.
Solution - Add SET NOCOUNT ON
Therefore the solution is simple, make sure all stored procedures have this command set at the top of them. However if you have a large database containing hundreds of stored procedures then this would be quite a task to carry out manually. Therefore I came up with a way to automate this task when upgrading large database systems by using the system views available in SQL Server.
The idea is pretty simple.
- Find out which stored procedures do not contain the SET NOCOUNT statement.
- Script out those stored procedures and update them to contain the statement.
- Run the script to ALTER the stored procs and update the database
- Use standard functions and pure TSQL to accomplish the task.
Syscomments and Sysobjects
In SQL 2000 and 2005 all stored procedure and user defined functions have their source code available for viewing from the syscomments system view. The system views cannot be updated directly so its a case of using them to generate ALTER statements to run if anything requires changing.
You can view all the stored procedures without the SET NOCOUNT statement with the following SQL.
SELECT c.*,o.*
FROM sys.syscomments as c
JOIN sys.sysobjects as o
ON o.id = c.id
WHERE xtype='P' --stored procs
AND Name LIKE 'usp_%' --my prefix
AND LOWER(text) NOT LIKE '%set nocount on%'
ORDER BY o.Name
Solution to the problem
The final script I used to solve this solution can be accessed here:
Download SQL script to insert missing SET NOCOUNT ON statements to stored procedures
I have split it into two separate loops mainly to get round limitations of displaying large strings from variables in query analyser and the system stored procedure I have used to return the complete code from syscomments which is called sp_helpText.
There are multiple ways that this task could have been achieved but I wanted to keep it a purely TSQL solution and although its not the most elegant piece of code ever writen it has done the job it was designed to do which was to update 100+ stored procedures so that I didn't have to do it by hand.
To view more solutions to common database problems that I have solved by using the system views please see
http://blog.strictly-software.com/2009/02/sql-system-views.html
http://blog.strictly-software.com/2009/03/sql-server-2005-script-fulltext-indexes.html
http://blog.strictly-software.com/2008/09/script-find-text-in-database.html
Labels: ALTER, script, set nocount, stored procedure, syscomments, sysobjects, view


2 Comments:
Excellent job, placing automatically set nocount ons to sps was the thing I've been searching for.We will make this script run for nearly 6000 sps !
Thus it was very usefull.Thanks a lot.
Hi again, I'm writing to you for the 2nd time today.I found a bug in the script.You have to insert an extra NL after the command:
INSERT #PROC EXEC sp_helpText @ProcName
otherwise, in same cases, it removes the last line of the original script.
Take care.
Post a Comment
Subscribe to Post Comments [Atom]
Links to this post:
Create a Link
<< Home