SQL nocount statement

Source: Internet
Author: User

SQL nocount statement

Default category
09:19:57 read 205 comments 0 font size: large and small subscriptions

Every time we use the query analyzer to debug SQL statements, we usually see some information, reminding us how many rows are affected currently. What information is this? Is this information useful when we call it? Can I disable it?

The answer is the application of this information on our client.ProgramThe information is the done_in_proc information of each statement in the stored procedure.

We can use set
Nocount to control this information to improve program performance.

The help in msdn is as follows:


Set nocount

The returned results do not contain information about the number of rows affected by the transact-SQL statement.

Syntax

Set nocount {on | off}

Note

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
When nocount is on, @ rowcount is also updated.
Function.

When set nocount
When the value is on
Done_in_proc. When you use a utility provided by Microsoft SQL server to execute a query
Delete) at the end of the query, "Nn rows affected" is not displayed in the query results ".

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.

Permission

The Set nocount permission is granted to all users by default.

Conclusion: We should add set nocount on to the header of the stored procedure, and add set
Nocount off to optimize the stored procedure.

Let's say two more words:

1: when viewing sqlserver's help, pay attention to the "permission" section, because some statements require certain permissions, which are often ignored.

2: @ rowcount refers to the number of rows affected by the previous statement, including the number of records found, the number of deleted rows, and the number of updated records, and @ rowcount should follow the statement to be judged; otherwise, @ rowcount will return 0.

3: If you use a table variable, you must use an alias in the conditional expression to represent the name. Otherwise, an error is reported.

4: transactions must be processed in cud operations.

5: Use an error handler to check that the T-SQL Statement (IF) of the @ error system function is actually clearing the @ error value in the process, no value except zero can be captured, must use set or select
Capture errors nowCode.

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.