计算已删除行的数量

时间:2014-03-05 10:19:18

标签: sql-server tsql

如何在SQL Server中使用存储过程时计算已删除行的数量?
我试着以下:

ALTER procedure [dbo].[P_DeleteNames] @xmlDoc nText
as
declare @handle int 
exec sp_xml_preparedocument @handle output, @xmlDoc
create table #deletename(nameId int) 
insert INTO #deletename(nameId)
SELECT NameID
from openxml(@handle,'/dsNames/dtNames',2)
with (nameID int)

set NOCOUNT OFF
set ROWCOUNT 0
delete FROM NamesTable where name_ID in(select nameID from #deletename)  
exec sp_xml_removedocument @handle 

它始终返回已删除行的两倍的数字。我无法弄清楚

1 个答案:

答案 0 :(得分:0)

试一试:

ALTER procedure [dbo].[P_DeleteNames] @xmlDoc nText
as
declare @handle int 
declare @countdeleted int

exec sp_xml_preparedocument @handle output, @xmlDoc


delete FROM NamesTable where name_ID in(
 SELECT NameID
 from openxml(@handle,'/dsNames/dtNames',2)
 with (nameID int)
)

/* save in a local variable and use it as you wish */
set @countdeleted = @@ROWCOUNT

exec sp_xml_removedocument @handle

@@ROWCOUNT是一个全局变量,它会计算受最后一个命令影响的行。