Statement包含一个OUTPUT子句,在SQL Server 2012中使用触发器时没有INTO子句错误?

时间:2017-07-26 05:15:32

标签: sql-server sql-server-2008 sql-server-2012

我有一个审核记录插入,更新和删除的触发器。我的代码是

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[mytable_insertafter_audit]
ON [dbo].[mytable]
AFTER INSERT, DELETE, UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    DECLARE @Action as char(1);
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM INSERTED)
                         AND EXISTS(SELECT * FROM DELETED)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM INSERTED)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM DELETED)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)

    IF (@Action = 'I')
    BEGIN
        INSERT INTO audit_trg
            SELECT 
                lr.sanction_status, i.sanction_status,
                GETDATE(), lr.id, 'mytable', @Action 
            FROM
                mytable lr
            INNER JOIN
                INSERTED i ON i.id = lr.id
    END
    ELSE IF (@Action = 'U')
    BEGIN
        INSERT INTO audit_trg
            SELECT
                i.sanction_status, lr.sanction_status,
                GETDATE(), lr.id, 'mytable', @Action 
            FROM
                mytable lr
            INNER JOIN 
                DELETED i ON i.id = lr.id
    END
    ELSE
    BEGIN
        INSERT INTO audit_trg
            SELECT
                lr.sanction_status, i.sanction_status,
                GETDATE(), lr.id, 'mytable', @Action 
            FROM 
                mytable lr
            INNER JOIN
                DELETED i ON i.id = lr.id
    END
END

和audit_trg表

CREATE TABLE [dbo].[audit_trg]
(
    [id] [int] IDENTITY(1,1) NOT NULL,
    [old_status] [varchar](50) NULL,
    [new_status] [varchar](50) NULL,
    [u_datetime] [datetime] NULL,
    [ref_id] [int] NULL,
    [table_name] [varchar](50) NULL,
    [actions] [varchar](50) NULL,

    CONSTRAINT [PK_audit_trg] 
        PRIMARY KEY CLUSTERED ([id] ASC)
) ON [PRIMARY]

但是在插入一些

之后我收到错误
  

DML语句的目标表'dbo.mytable'不能有   如果语句包含OUTPUT子句,则启用任何触发器   没有INTO条款。

0 个答案:

没有答案