1

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

提前道歉,但我对这种东西非常陌生!

就目前而言,我的触发器如下:

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.

这似乎是“插入审计”命令,但我不知道从这里做什么!

任何帮助将不胜感激,在此先感谢!

4

2 回答 2

2

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

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

重点补充)

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

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

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

现在,我的另一个观察结果是,所有这些与条件控制流有关的事情都是毫无意义的——0insert行将没有任何效果,而对空表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
于 2012-07-09T09:35:18.130 回答
0
 insert into Audit
        select *, getdate() as CreateDate, null
        from inserted

您的“审计”表的列数比您插入的多(删除日期);您需要命名所有列,或插入空值。

从风格上讲,为列命名更好——它可以清楚地说明什么在哪里,并在添加新列时避免错误。

于 2012-07-09T09:35:36.050 回答