2009-07-09 70 views
60

如何獲取作爲結果集的存儲過程中的UPDATE查詢影響的行數(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 

回答

85
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 
8

您可能需要收集的統計數據,當您去,但@@ROWCOUNT抓住了這個:

declare @Fish table (
Name varchar(32) 
) 

insert into @Fish values ('Cod') 
insert into @Fish values ('Salmon') 
insert into @Fish values ('Butterfish') 
update @Fish set Name = 'LurpackFish' where Name = 'Butterfish' 
select @@ROWCOUNT --gives 1 

update @Fish set Name = 'Dinner' 
select @@ROWCOUNT -- gives 3 
9

這也正是OUTPUT條款在SQL Server 2005之後是非常好的。

CREATE TABLE [dbo].[test_table](
    [LockId] [int] IDENTITY(1,1) NOT NULL, 
    [StartTime] [datetime] NULL, 
    [EndTime] [datetime] NULL, 
PRIMARY KEY CLUSTERED 
(
    [LockId] ASC 
) ON [PRIMARY] 
) ON [PRIMARY] 

INSERT INTO test_table(StartTime, EndTime) 
VALUES('2009 JUL 07','2009 JUL 07') 
INSERT INTO test_table(StartTime, EndTime) 
VALUES('2009 JUL 08','2009 JUL 08') 
INSERT INTO test_table(StartTime, EndTime) 
VALUES('2009 JUL 09','2009 JUL 09') 
INSERT INTO test_table(StartTime, EndTime) 
VALUES('2009 JUL 10','2009 JUL 10') 
INSERT INTO test_table(StartTime, EndTime) 
VALUES('2009 JUL 11','2009 JUL 11') 
INSERT INTO test_table(StartTime, EndTime) 
VALUES('2009 JUL 12','2009 JUL 12') 
INSERT INTO test_table(StartTime, EndTime) 
VALUES('2009 JUL 13','2009 JUL 13') 

UPDATE test_table 
    SET StartTime = '2011 JUL 01' 
    OUTPUT INSERTED.* -- INSERTED reflect the value after the UPDATE, INSERT, or MERGE statement is completed 
WHERE 
    StartTime > '2009 JUL 09' 

導致以下返回

LockId StartTime    EndTime 
------------------------------------------------------- 
4  2011-07-01 00:00:00.000 2009-07-10 00:00:00.000 
5  2011-07-01 00:00:00.000 2009-07-11 00:00:00.000 
6  2011-07-01 00:00:00.000 2009-07-12 00:00:00.000 
7  2011-07-01 00:00:00.000 2009-07-13 00:00:00.000 

你的具體情況,因爲你需要捕獲的INSERTED.*輸出在一個表中不能使用聚合函數OUTPUT變量或臨時表並計數記錄。例如,

DECLARE @temp TABLE (
    [LockId] [int], 
    [StartTime] [datetime] NULL, 
    [EndTime] [datetime] NULL 
) 

UPDATE test_table 
    SET StartTime = '2011 JUL 01' 
    OUTPUT INSERTED.* INTO @temp 
WHERE 
    StartTime > '2009 JUL 09' 


-- now get the count of affected records 
SELECT COUNT(*) FROM @temp 
+0

`OUTPUT COUNT(INSERTED。*)`導致'*'附近的語法不正確``。 – 2014-03-18 01:02:46

1

確保列類型支持您嘗試傳遞給它的值! 有同樣的問題,因爲我試圖更新13(INT)字段寬度的13位數字值。

相關問題