[英]getting number of records updated or inserted in sql server stored procedure
I have an SP that inserts some records and updates others and deletes some. 我有一个SP插入一些记录并更新其他记录并删除一些。 What I want is to return the count values of what was inserted and what was updated and what was deleted.
我想要的是返回插入的内容,更新内容和删除内容的计数值。 I thought I could use @@ROWCOUNT but that is always giving me a 1.
我以为我可以使用@@ ROWCOUNT,但这总是给我一个1。
After my INSERT I run: 我的INSERT之后运行:
PRINT @@ROWCOUNT
But my message console shows what really happened and this number: 但我的消息控制台显示了真正发生的事情和这个数字:
(36 row(s) affected)
1
So I can see that 36 records were actually updated but @@ROWCOUNT returned a 1. 所以我可以看到36条记录实际更新了,但@@ ROWCOUNT返回1。
I am trying to do the same thing after the UPDATE and DELETE parts of the SP runs with the same result. 我试图在SP的UPDATE和DELETE部分以相同的结果运行之后做同样的事情。
@@ROWCOUNT
will show the number of rows affected by the most recent statement - if you have any statements between the INSERT
and the PRINT
then it will give you the wrong number. @@ROWCOUNT
将显示受最新语句影响的行数 - 如果INSERT
和PRINT
之间有任何语句,则它将给出错误的数字。
Can you show us a little more code so we can see the order of execution? 你能告诉我们更多的代码,以便我们看到执行的顺序吗?
Depending on how @ninesided's answer works for you, you could also use the output
clause on each update/insert/delete and get the counts from there. 根据@ninesided的答案如何为您工作,您还可以在每次更新/插入/删除时使用
output
子句,并从那里获取计数。
Example: 例:
declare @count table
(
id int
)
update mytable
set oldVal = newVal
output inserted.field1 into @count
select count(*) from @count
You could reuse the count table throughout, and set variables as needed to hold the values. 您可以重复使用计数表,并根据需要设置变量来保存值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.