SetRowCount and topn

Source: Internet
Author: User
Sometimes, using n in topn is a variable, you need to use () to complete: declare @ count1intset @ count18selecttopstrong (@ count1) strong * fromMyTable can achieve the same effect if setRowCount is used. declare @ count1intset @ count18setRowCount @ count1s

Sometimes, if n in top n is a variable, you need to use (): declare @ count1 intset @ count1 = 8 select top strong (@ count1) /strong * from MyTable can achieve the same effect if set RowCount is used. declare @ count1 intset @ count1 = 8 set RowCount @ count1s

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

declare @count1 intset @count1 = 8select top (@count1) * 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 ).

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.