gpt4 book ai didi

sql - 返回受 UPDATE 语句影响的行数

转载 作者:行者123 更新时间:2023-12-01 17:04:26 24 4
gpt4 key购买 nike

如何获取存储过程 (SQL Server 2005) 中受 UPDATE 查询影响的行数作为结果集。例如

CREATE PROCEDURE UpdateTables
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

UPDATE Table1 Set Column = 0 WHERE Column IS NULL
UPDATE Table2 Set Column = 0 WHERE Column IS NULL
UPDATE Table3 Set Column = 0 WHERE Column IS NULL
UPDATE Table4 Set Column = 0 WHERE Column IS NULL
END

然后返回:

Table1    Table2    Table3    Table4
32 45 0 3

最佳答案

CREATE PROCEDURE UpdateTables
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @RowCount1 INTEGER
DECLARE @RowCount2 INTEGER
DECLARE @RowCount3 INTEGER
DECLARE @RowCount4 INTEGER

UPDATE Table1 Set Column = 0 WHERE Column IS NULL
SELECT @RowCount1 = @@ROWCOUNT
UPDATE Table2 Set Column = 0 WHERE Column IS NULL
SELECT @RowCount2 = @@ROWCOUNT
UPDATE Table3 Set Column = 0 WHERE Column IS NULL
SELECT @RowCount3 = @@ROWCOUNT
UPDATE Table4 Set Column = 0 WHERE Column IS NULL
SELECT @RowCount4 = @@ROWCOUNT

SELECT @RowCount1 AS Table1, @RowCount2 AS Table2, @RowCount3 AS Table3, @RowCount4 AS Table4
END

关于sql - 返回受 UPDATE 语句影响的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1103260/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com