Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of deleted rows in a SQL Server stored procedure

In SQL Server 2005, is there a way of deleting rows and being told how many were actually deleted?

I could do a select count(*) with the same conditions, but I need this to be utterly trustworthy.

My first guess was to use the @@ROWCOUNT variables - but that isn't set, e.g.

delete  from mytable  where datefield = '5-Oct-2008'   select @@ROWCOUNT  

always returns a 0.

MSDN suggests the OUTPUT construction, e.g.

delete from mytable  where datefield = '5-Oct-2008'  output datefield into #doomed  select count(*)  from #doomed 

this actually fails with a syntax error.

Any ideas?

like image 294
Unsliced Avatar asked Oct 06 '08 12:10

Unsliced


People also ask

How can I get the number of rows deleted in SQL Server?

As in, SET v_count = @@ROWCOUNT. below is the pl-sql code. It, displays the number of total records inserted/updated/deleted. If the count matches, will issue COMMIT otherwise rollback.

How can I get the number of records affected by a stored procedure?

Use SQL%ROWCOUNT if you are using Oracle. Mind that if you have multiple INSERT/UPDATE/DELETE , you'll need a variable to store the result from @@ROWCOUNT for each operation. Show activity on this post. @@RowCount will give you the number of records affected by a SQL Statement.

How can use stored procedure Rowcount in SQL Server?

Usage. SQL Server @@ROWCOUNT is a system variable that is used to return the number of rows that are affected by the last executed statement in the batch.


1 Answers

Have you tried SET NOCOUNT OFF?

like image 120
wcm Avatar answered Oct 23 '22 02:10

wcm