Set rowcount and Top N

Source: Internet
Author: User

Sometimes, if n in Top N is a variable, you need to use:

declare @count1 intset @count1 = 8select top <strong>(@count1)</strong> * from MyTable


If set rowcount is used, the same effect can be achieved.

declare @count1 intset @count1 = 8        set RowCount @count1select * from MyTable

Note that using set rowcount has a side effect. Once set, the subsequent statements will be affected, and SQL server will change this in subsequent versions, so be careful when using it.
Important:
In the next version of SQL Server, using set rowcount does not affect the delete, insert, and update statements. In the new development work, avoid using the set rowcount statement with the delete, insert, and update statements, and plan to modify the application currently using the statement. In addition, for the delete, insert, and update statements currently using set rowcount, we recommend that you use top syntax to overwrite them. For more information, see Delete (TRANSACT-SQL), insert (TRANSACT-SQL), or update (TRANSACT-SQL ).

Set rowcount and Top N

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.