See Also
You are here: SQL Reference > SQL Functions and Operators > Scope Functions > @@ROWCOUNT
ContentsIndexHome
PreviousUpNext
@@ROWCOUNT
@@ROWCOUNT

Returns a 64 bit integer representing the number of rows affected by the last SQL statement. Data manipulation language (DML) statements set the @@ROWCOUNT value to the number of rows affected by the UPDATE/DELETE/INSERT or by SELECT, if output is not sent to the client. 

 

Examples 

Simple select:

select count() from system.systables
select @@rowcount
-> 64

 

Create Database & table if not exists. Update row or ,insert if row exists:

-- create database
declare @dbid bigint;
select @dbid=id from system.syscatalogs where name = 'test'
if @@rowcount = 0 then CREATE DATABASE test
go;

use test;
go;

-- create table
select * from system.systables where name = 'test_1234' and catalog_id=@dbid;
if @@rowcount = 0 then
    create table test_1234
    (
    id int not null primary key,
    cnt int
    )
go;

-- update row
update test_1234
set cnt = cnt+1
where id = 0

-- insert row if update did not found any rows
if @@rowcount = 0 then
begin
    insert into test_1234 values(0,0);
    select 0 as LastValue
end
else
    -- return of the last modified row’s [cnt] column’s value
    select last_row(cnt) as LastValue

->0
->1
…
->N