Use of ROWCOUNT

Introduction:

ROWCOUNT uses to stop processing the query after the specified number of rows is returned means its returned rows by query as Top keyword in sql.

Data for Query:

SELECT Id,Name,SALARY FROM EMPLOYEE

empData.PNG

Use Top Keyword:

Query uses top keyword to return specified number of rows.

SELECT TOP 5 Id,Name,SALARY FROM EMPLOYEE

empTopData.PNG

Use RowCOUNT:

DECLARE @NoOfRows INT = 5

SET ROWCOUNT @NoOfRows

SELECT Id,Name,SALARY FROM EMPLOYEE

Here ROWCOUNT uses to stop processing the query after the specified number of rows is returned. When we use  more than one select statement then both select statement will work according to ROWCOUNT means both select statement will have effect of ROWCOUNT and both select statement will return fix according to ROWCOUNT.

empTopData.PNG

Use @@ROWCOUNT:

It returns number of affected rows by last statement  but it does not return this value to client.

DECLARE @NoOfRows INT = 5

SET ROWCOUNT @NoOfRows

SELECT Id,Name,SALARY FROM EMPLOYEE

SELECT @@ROWCOUNT AS [Number Of Rows]

noOfrows.PNG