Reprint the usage of rowcount and @ @Rowcount in--sql server

Source: Internet
Author: User
Tags rowcount

Reprinted from: http://www.lmwlove.com/ac/ID943

Usage of ROWCOUNT:


The function of rowcount is to restrict the subsequent SQL to stop processing after returning the specified number of rows, such as the following example,

SET ROWCOUNT 10
SELECT * FROM Table A


Such a query will only return the first 10 data in table A. It acts as a "select top * from Table a". Note that SET ROWCOUNT settings are valid throughout the session. such as the following SQL example:

SET ROWCOUNT 10
SELECT * FROM Table A
Go
SELECT * FROM Table B


Both table A and Table B return only the first 10 data.
To cancel the SET ROWCOUNT qualification, SET ROWCOUNT 0 is all you need.

From the above example, it seems that rowcount does not have much use, limit the data of the query results, we use top to be able to, and do not have to worry about if forget to cancel the rowcount's setting and the effect on the subsequent SQL. But in the following circumstances, rowcount's settings will bring us a lot of convenience oh.

We all know that no arguments can be added to the select top, only a number with a specific int type. If we want to implement the function of the parameter in the back of top, we only construct the SQL string and then use exec to execute it. Like what:

DECLARE @n int
declare @sql nvarchar (1000)
Set @n=10
Set @sql = ' Select Top ' +cast (@n as varchar) + ' * FROM Table a '
EXEC (@sql)


Not to mention the performance of exec in the above statement, from the readability of SQL is very unfriendly. But if we use rowcount to solve this, it will be very elegant, because the SET rowcount can use parameters later. Examples are as follows:

DECLARE @n int
Set @n=10
SET ROWCOUNT @n
SELECT * FROM Table A


Note:the qualification of SET ROWCOUNT is as valid as the modification and deletion. such as the following example:

SET ROWCOUNT 10
Update Table A set qty=10 where id<100


In this way, the above statement modifies only the first 10 data of id<100 in table A (assuming that id<100 has more than 10 data)

Delete is the same

SET ROWCOUNT 10
Delete from Table A


In this way, the above statement will only delete the first 10 data in table A.

How to use @ @Rowcount


@ @Rowcount and rowcount look like, only two different @, but their function is not the same, @ @Rowcount is mainly to return the last SQL statement affected by the number of rows of data, such as:

Select top 2 * FROM Table A
SELECT @ @Rowcount


If the amount of data in table A is greater than or equal to 2, then the SELECT @ @Rowcount returns 2, and if there is only 1 or 0 data, then the SELECT @ @Rowcount returns 1 or 0.

Note that the @ @Rowcount is not understood to return only the number of results of the query, delete, modify, add new statements, and return the @ @Rowcount value correctly. For example:

Update table A set gid= ' a ' where gid= ' a '
SELECT @ @Rowcount


If the data for gid= ' a ' exists in table A, then the SELECT @ @Rowcount returns the number of rows of data it modifies, and if there is no data for gid= ' A ', then the SELECT @ @Rowcount returns 0, and the deletion is the same as the new one.

So, where do we use @ @Rowcount?
One, may we see @ @Rowcount figure Most of the place is the trigger, a good trigger, usually at the front with the IF @ @rowcount =0 return statement, such as:

Create trigger Ti_tablea on TableA after update
As
If @ @rowcount =0 return
......


Thus, if the number of data rows modified by TableA is 0, then the trigger Ti_tablea exits without executing the subsequent code.

The second possible place is that we can use @ @rowcount for recursion or looping. such as the following example:

DECLARE @n int
Set @n=1
SELECT * from client_goods where [email protected]

While @ @rowcount >0
Begin
Set @[email protected]+1
SELECT * from client_goods where [email protected]
End


This example is to query client_goods whether there is id=1 data, if any, and then query whether there is id=2 data, continue to check until the ID is not continuous. Of course, when you look at this example, do not consider the meaning of this example, it just shows that @ @rowcount can be used as a cyclic condition.

RowCount and @ @rowcount other uses, you are welcome to add in the comments.

Source:. NET Learning Network
Description: All sources are. NET Learning NetworkArticles are original, if reproduced, please mark this page address in the reprint, thank you!

Reprint the usage of rowcount and @ @Rowcount in--sql server

Related Article

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.