2012-02-29 19 views
0

我已经写了一个更新触发器,当我更新只有一行,但更新多行时发生错误时工作正常。如何为多行更新写入触发器?

错误:

Msg 512, Level 16, State 1, Procedure Sale_OnUpdate, Line 14 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

这里是触发

ALTER TRIGGER [dbo].[Sale_OnUpdate] ON [dbo].[Sale] 
    AFTER Update 

AS 
Declare @ID as decimal 
Declare @User as varchar(250) 
Declare @Status as varchar(250) 

set @ID = (Select ID from Inserted) 
set @User = (Select UpdatedByUser from Inserted) 
set @Status = Isnull((Select Status from Inserted),'') 

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

    INSERT INTO [dbo].[Log] 
      (
      [RecordID] 
      ,[Date] 
      ,[Time] 
      ,[UserName] 
      ,[TableName] 
      ,[Action] 
      ) 
    VALUES 
      (
      @ID 
      ,GetDate() 
      ,GetDate() 
      ,@User 
      ,'Sale' 
      ,'Update,' + @Status 
      ) 
END 

我应该做的改变,使之成为多行工作。

回答

0

用途:

ALTER TRIGGER [dbo].[Sale_OnUpdate] ON [dbo].[Sale] 
    AFTER Update 

AS 

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

    INSERT INTO [dbo].[Log] 
    ([RecordID] 
    , [Date] 
    , [Time] 
    , [UserName] 
    , [TableName] 
    , [Action]) 
    SELECT i.id, 
     GETDATE(), 
     GETDATE(), 
     i.updatedbyuser, 
     'Sale', 
     'Update,' + ISNULL(i.status, '') 
    FROM INSERTED i 

END 
+0

但我想让这三个字段(ID,用户,状态)登录。 – 2012-02-29 05:10:16

+0

@ MujassirNasir:那不是你的问题。 – 2012-02-29 05:11:03

+0

我试过这个我知道它的作品,但我想写触发器来记录每一个动作。 – 2012-02-29 05:13:00

1
ALTER TRIGGER [dbo].[Sale_OnUpdate] ON [dbo].[Sale] 
    AFTER Update 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    INSERT INTO [dbo].[Log] 
      (
      [RecordID] 
      ,[Date] 
      ,[Time] 
      ,[UserName] 
      ,[TableName] 
      ,[Action] 
      ) 
    SELECT 
      ID 
      ,GetDate() 
      ,GetDate() 
      ,UpdatedByUser 
      ,'Sale' 
      ,'Update,' + Isnull(Status,'') 
    from Inserted 
END 
+0

但我想让这三个字段(ID,用户,状态)登录。 – 2012-02-29 05:09:31

+0

@MujassirNasir这正是修改后的触发器所做的事情(除了使用UpdatedByUser而不是User,如您在原始问题中指定的那样)。仔细检查SELECT子句。 – 2012-02-29 05:11:58

1

试试这个

ALTER TRIGGER [dbo].[Sale_OnUpdate] ON [dbo].[Sale] 
    AFTER Update 

AS 
as 
if update (qty) 


BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements. 
    SET NOCOUNT ON; 
/* check value of @@rowcount */ 
    if @@rowcount = 1 

    INSERT INTO [dbo].[Log] 
    ([RecordID] 
    , [Date] 
    , [Time] 
    , [UserName] 
    , [TableName] 
    , [Action]) 
    SELECT id, 
     GETDATE(), 
     GETDATE(), 
     updatedbyuser, 
     'Sale', 
     'Update,' + ISNULL(status, '') 

    else 
    /* when rowcount is greater than 1, 
     use a group by clause */ 
    begin 
    INSERT INTO [dbo].[Log] 
    ([RecordID] 
    , [Date] 
    , [Time] 
    , [UserName] 
    , [TableName] 
    , [Action]) 
    SELECT i.id, 
     GETDATE(), 
     GETDATE(), 
     i.updatedbyuser, 
     'Sale', 
     'Update,' + ISNULL(i.status, '') 
    FROM INSERTED i 
    and inserted.title_id = deleted.title_id 
    end 

END 
0

我也有类似的问题,这个想法在这里是插入的表有许多记录,所以如果你想要分析的每个值,您将需要通过示例使用游标迭代表格:

Loop through INSERTED table