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

前端 未结 7 1747
借酒劲吻你
借酒劲吻你 2021-02-06 20:26

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 s

7条回答
  •  攒了一身酷
    2021-02-06 21:21

    I found a case where you can't use @@rowcount, like when you want to know the distinct count of the values that were deleted instead of the total count. In this case you would have to do the following:

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

    The syntax error in the OP was because output did not include deleted before the datefield field name.

提交回复
热议问题