审核触发器 - 列名称或提供的值数与表定义不匹配

时间:2012-07-09 09:16:32

标签: sql triggers audit

我正在尝试创建一个数据库触发器,该触发器能够创建“事务”表的快照并将其存储在“审计”表中,以及与插入,更新或删除任何数据行的日期有关的信息

提前道歉,但我非常喜欢这种东西!

目前,我的触发器如下:

create trigger AuditTransactions
on dbo.Transactions
after insert, update, delete
as

    if exists(select * from inserted)
    begin
        if exists(select * from deleted)
        begin
            -- this is for an update
            update Audit
            set DeleteDate = getdate()
            where TransactionId in (select TransactionId from deleted)
        end 

        -- this is just for an insert
        insert into Audit
        select *, getdate() as CreateDate
        from inserted
    end
    else
    begin
        -- this is just for a delete
        update Audit
        set DeleteDate = getdate()
        where TransactionId in (select TransactionId from deleted)
    end

go

我的审计表如下所示:

CREATE TABLE [dbo].[Audit](
    [TransactionId] [int] NOT NULL,
    [InvoiceNumber] [nvarchar](1) NULL,
    [InvoiceType] [nvarchar](1) NULL,
    [InvoiceIssueDate] [datetime] NULL,
    [InvoiceTotalexclVat] [nvarchar](1) NULL,
    [InvoiceTotalVat] [numeric](18, 0) NULL,
    [InvoiceDiscount] [numeric](18, 0) NULL,
    [InvoiceTotalPayable] [numeric](18, 0) NULL,
    [AccountCode] [nvarchar](1) NULL,
    [Reference1] [nvarchar](1) NULL,
    [Reference2] [nvarchar](1) NULL,
    [Reference3] [nvarchar](1) NULL,
    [Level1CustomOrg] [nvarchar](1) NULL,
    [Level2CustomOrg] [nvarchar](1) NULL,
    [Level3CustomOrg] [nvarchar](1) NULL,
    [Level4CustomOrg] [nvarchar](1) NULL,
    [ScanLocation] [nvarchar](1) NULL,
    [ScanDateTime] [datetime] NULL,
    [CaptureInkjetId] [nvarchar](1) NULL,
    [CaptureBatchId] [nvarchar](1) NULL,
    [CaptureDateTime] [datetime] NULL,
    [InputSource] [nvarchar](1) NULL,
    [CurrencyCode] [nvarchar](1) NULL,
    [DebitCredit] [nvarchar](1) NULL,
    [OrderNumberHeader] [nvarchar](1) NULL,
    [SupplierName] [nvarchar](1) NULL,
    [BancPaySupplierId] [nvarchar](1) NULL,
    [SupplierIDERP] [nvarchar](1) NULL,
    [PaymentDate] [datetime] NULL,
    [DeliveryDate] [datetime] NULL,
    [CustomRef1] [nvarchar](1) NULL,
    [CustomRef2] [nvarchar](1) NULL,
    [CustomRef3] [nvarchar](1) NULL,
    [CustomRef4] [nvarchar](1) NULL,
    [CustomRef5] [nvarchar](1) NULL,
    [CustomRef6] [nvarchar](1) NULL,
    [CustomRef7] [nvarchar](1) NULL,
    [CustomRef8] [nvarchar](1) NULL,
    [CustomRef9] [nvarchar](1) NULL,
    [CustomRef10] [nvarchar](1) NULL,
    [CustomRef11] [nvarchar](1) NULL,
    [CustomRef12] [nvarchar](1) NULL,
    [CustomRef13] [nvarchar](1) NULL,
    [CustomRef14] [nvarchar](1) NULL,
    [CustomRef15] [nvarchar](1) NULL,
    [CustomAmount1] [numeric](18, 0) NULL,
    [CustomAmount2] [numeric](18, 0) NULL,
    [CustomAmount3] [numeric](18, 0) NULL,
    [CustomAmount4] [numeric](18, 0) NULL,
    [CustomAmount5] [numeric](18, 0) NULL,
    [CustomDate1] [datetime] NULL,
    [CustomDate2] [datetime] NULL,
    [Country1] [nvarchar](1) NULL,
    [Country2] [nvarchar](1) NULL,
    [Country3] [nvarchar](1) NULL,
    [Country4] [nvarchar](1) NULL,
    [Country5] [nvarchar](1) NULL,
    [Country6] [nvarchar](1) NULL,
    [Country7] [nvarchar](1) NULL,
    [Country8] [nvarchar](1) NULL,
    [Country9] [nvarchar](1) NULL,
    [Country10] [nvarchar](1) NULL,
    [CheckedOut] [bit] NULL,
    [CheckedOutDate] [datetime] NULL,
    [BlobUrl] [nvarchar](1) NULL,
    [GLCode] [nvarchar](1) NULL,
    [RejectReason] [nvarchar](1) NULL,
    [RejectComment] [nvarchar](1) NULL,
    [ReferMessage] [nvarchar](1) NULL,
    [PaymentTerms] [nvarchar](1) NULL,
    [CheckedOutByUserId] [int] NULL,
    [LastUpdatedByUserId] [int] NULL,
    [TransactionFormatTypeId] [int] NULL,
    [RequestOriginalStatusTypeId] [int] NULL,
    [GLCodeComment] [nvarchar](1) NULL,
    [SenderOrganizationId] [int] NULL,
    [ReceiverOrganizationId] [int] NULL,
    [TransactionStatusTypeId] [int] NULL,
    [TransactionTypeId] [int] NULL,
    [OrganizationId] [int] NULL,
    [OrganizationId1] [int] NULL,
    [CreateDate] [datetime] NOT NULL,
    [DeleteDate] [datetime] NULL
) ON [PRIMARY]

GO

尝试执行触发器的查询时,我收到以下错误消息:

Msg 213, Level 16, State 1, Procedure AuditTransactions, Line 17
Column name or number of supplied values does not match table definition.

这似乎是'插入审核'命令,但我不知道该怎么做!

非常感谢任何帮助,提前谢谢!

2 个答案:

答案 0 :(得分:2)

INSERT似乎记录得很差。对于VALUES()元素,它声明如下:

  

如果值列表中的值与表格中的列的顺序不同,或表格中的每列没有值,则必须使用column_list显式指定存储每个传入值的列。

强调已添加)

然而,我的理解和信念是,无论值/行的来源是什么,相同的约束都适用 - 除非您完全匹配表中的所有列,否则您需要提供column_list

现在,对于您的实例,如果您只为表中的每个列提供一个值,可能会更容易:

insert into Audit
select *, getdate() as CreateDate,null as DeleteDate
from inserted

现在,我的另一个观察是,所有关于条件控制流的问题都是毫无意义的 - {0}行的insert无效,而update使用in反对空表同样没有效果。所以我只有:

create trigger AuditTransactions
on dbo.Transactions
after insert, update, delete
as

    update Audit
    set DeleteDate = getdate()
    where TransactionId in (select TransactionId from deleted)

    insert into Audit
    select *, getdate() as CreateDate,null as DeleteDate
    from inserted

答案 1 :(得分:0)

 insert into Audit
        select *, getdate() as CreateDate, null
        from inserted

您的“审核”表包含的列数多于您插入的列数(已删除的日期);您需要为所有列命名,或者插入空值。

在风格上,命名列更好 - 它可以清楚地说明了哪些内容,并在添加新列时避免错误。