Set RowCount and Top n

Source: Internet
Author: User
Tags rowcount

Sometimes, with top n middle n It is a variable that will need to be used () to complete:

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


Assuming that the same effect can be achieved with SET ROWCOUNT

DECLARE @count1 intset @count1 = 8        Set RowCount @count1select * from MyTable

But be careful. Using SET rowcount has side effects, once set. The subsequent statements will affect the. and SQL Server changes this in the likely version number. So be careful with it.
Important NOTES:
In the next version number of SQL Server, using set ROWCOUNT will 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 change the application that currently uses the statement.

In addition, for DELETE, INSERT, and UPDATE statements that are currently using SET ROWCOUNT, it is recommended that you rewrite them using the TOP syntax. For specific information, see DELETE (Transact-SQL), INSERT (Transact-SQL), or UPDATE (Transact-SQL).


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

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.