您好我想要计算下面脚本中受影响的行数
BEGIN TRANSACTION;
BEGIN TRY
USE db1
PRINT 'DELETING STUFF'
DELETE FROM oc WHERE a_col LIKE 'G' AND b_col LIKE '11' AND c_col LIKE 'P' AND d_col LIKE 'N'
DELETE FROM oc WHERE a_col LIKE 'Z' AND b_col LIKE '1' AND c_col LIKE 'R' AND d_col LIKE 'G'
PRINT 'SCRIPT RETURNED 0 ERRORS'
END TRY
BEGIN CATCH
PRINT 'ERROR OCCURED WHILE DELETING, CHECK BELOW ERROR MESSAGE'
PRINT ERROR_MESSAGE()
END CATCH
ROLLBACK TRANSACTION;
它总是返回
(1行受影响)
对于每个删除语句,我正在寻找受影响的行数。无论如何要做到这一点?
答案 0 :(得分:0)
使用@@ROWCOUNT
:
DECLARE @num1 int;
DECLARE @num2 int;
DELETE FROM oc
WHERE a_col LIKE 'G' AND b_col LIKE '11' AND c_col LIKE 'P' AND d_col LIKE 'N';
SET @num1 = @@ROWCOUNT;
DELETE FROM oc WHERE a_col LIKE 'Z' AND b_col LIKE '1' AND c_col LIKE 'R' AND d_col LIKE 'G';
SET @num2 = @@ROWCOUNT;
答案 1 :(得分:0)
实际上,您的代码可以简化为单个删除语句:
@@ROWCOUNT
然后,您不需要显式交易,您可以直接从cats
获取受影响的行数。
答案 2 :(得分:0)
您还可以查看 OUTPUT
条款,该条款可以返回与 @@ROWCOUNT
DELETE FROM oc
OUTPUT DELETED.*
WHERE (a_col = 'G' AND b_col = '11' AND c_col = 'P' AND d_col = 'N') OR
(a_col = 'Z' AND b_col = '1' AND c_col = 'R' AND d_col = 'G')