Set nocount usage

Source: Internet
Author: User
When set nocount is on, no count is returned (indicating the number of rows affected by the transact-SQL statement ). When set nocount is off, return the count.
Even if set nocount is on, the @ rowcount function is updated.
When set nocount is on, the done_in_proc information of each statement in the stored procedure is not sent to the client. When Microsoft? SQL Server? When a query is executed using the provided utility (QA) "Nn rows affected" is not displayed in the query results at the end ".
If some statements contained in the stored procedure do not return much actual data, this setting greatly reduces network traffic and significantly improves performance.
Set nocount is set during execution or runtime, rather than during analysis.
If there are multiple statements in the stored procedure, SQL Server applies each statement to the client by default. Program Send a message detailing the number of lines affected by each statement. Most applications do not need these messages. If you are sure that applications do not need them, you can disable these messages to improve the performance of the slow network. We can use the set nocount session to disable these messages for the application. (This value is not required for most applications)
Note that:
1. Stored Procedure:
Like create table, record set is returned, and even the insert into statement is returned. However, this record set has a special record set (no field, you cannot perform any operation on this record set. There is a big difference between oledb and ODBC. When you fetch a record set from ODBC, ODBC filters out the special set of records called above (the set of records that only occupy the position but cannot perform any operation-mostly generated by create table or insert into), while the set of records is retrieved from oledb, oledb does not filter special record sets. Therefore, when using the stored procedure to return the record set, set nocount on should be used to prohibit the stored procedure from returning the record set. Otherwise, it may take a lot of detours.
2. Trigger:
We recommend that you do not have the trigger return any results. This is because the special processing of these returned results must be written to each application that allows modification to the trigger table. To prevent returning any results from the trigger, do not define a SELECT statement or assign values to variables in the trigger. If you must assign values to variables in the trigger, use the set nocount on statement at the beginning of the trigger to avoid returning any result set.
Today, I encountered a result set returned by the trigger, causing an error in deleting the trigger table. If one record can be deleted but multiple records cannot be deleted, the prompt is: Key column information SI insufficient or incorrect. too tables rows were affected by update.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.