删除 ... 输出计数(已删除。*)
DELETE ... OUTPUT COUNT(DELETED.*)
我想知道在某个 DELETE 操作中删除了多少行。
我拿了 the Microsoft example B 也就是
DELETE Sales.ShoppingCartItem
OUTPUT DELETED.*
WHERE ShoppingCartID = 20621;
并尝试将其修改为 return 只有 count
条已删除的记录:
DELETE FROM datacache
OUTPUT COUNT(DELETED.*)
WHERE userId=@id
但这抛出
ExceptionMessage: "Incorrect syntax near '*'."
ExceptionType: "System.Data.SqlClient.SqlException"
Message: "Error"
所以我尝试了
DELETE FROM datacache
OUTPUT COUNT(DELETED)
WHERE userId=@id
抛出
ExceptionMessage: "Invalid column name 'DELETED'."
ExceptionType: "System.Data.SqlClient.SqlException"
Message: "Error"
我错过了什么?
您不能在 OUTPUT
子句中使用聚合。您可以将任何列输出到 table 变量并从那里开始计数:
DECLARE @t TABLE(id int)
DELETE FROM Sales.ShoppingCartItem
OUTPUT Deleted.ShoppingCartID INTO @t
WHERE ShoppingCartID = 20621;
SELECT COUNT(*) FROM @t
之后统计记录如何?
DELETE Sales.ShoppingCartItem
OUTPUT DELETED.ID INTO @DELETEDIDS
WHERE ShoppingCartID = 20621;
SELECT COUNT(*)
FROM @DELETEDIDS;
或者,仅 运行 查询并使用 @@ROWCOUNT
。
只需 运行 您的查询并获取修改后的行
DELETE
FROM datacache
WHERE userId=@id
SELECT @@ROWCOUNT
我想知道在某个 DELETE 操作中删除了多少行。
我拿了 the Microsoft example B 也就是
DELETE Sales.ShoppingCartItem
OUTPUT DELETED.*
WHERE ShoppingCartID = 20621;
并尝试将其修改为 return 只有 count
条已删除的记录:
DELETE FROM datacache
OUTPUT COUNT(DELETED.*)
WHERE userId=@id
但这抛出
ExceptionMessage: "Incorrect syntax near '*'."
ExceptionType: "System.Data.SqlClient.SqlException"
Message: "Error"
所以我尝试了
DELETE FROM datacache
OUTPUT COUNT(DELETED)
WHERE userId=@id
抛出
ExceptionMessage: "Invalid column name 'DELETED'."
ExceptionType: "System.Data.SqlClient.SqlException"
Message: "Error"
我错过了什么?
您不能在 OUTPUT
子句中使用聚合。您可以将任何列输出到 table 变量并从那里开始计数:
DECLARE @t TABLE(id int)
DELETE FROM Sales.ShoppingCartItem
OUTPUT Deleted.ShoppingCartID INTO @t
WHERE ShoppingCartID = 20621;
SELECT COUNT(*) FROM @t
之后统计记录如何?
DELETE Sales.ShoppingCartItem
OUTPUT DELETED.ID INTO @DELETEDIDS
WHERE ShoppingCartID = 20621;
SELECT COUNT(*)
FROM @DELETEDIDS;
或者,仅 运行 查询并使用 @@ROWCOUNT
。
只需 运行 您的查询并获取修改后的行
DELETE
FROM datacache
WHERE userId=@id
SELECT @@ROWCOUNT