2012-07-09 3 views
1

'트랜잭션'테이블의 스냅 샷을 생성하고 '감사'테이블에 저장하는 데이터베이스 트리거를 생성하려고합니다 데이터 행 삽입, 갱신 또는 h 제 날짜까지.감사 트리거 - 제공된 값의 열 이름 또는 번호가 테이블 정의와 일치하지 않습니다.

사전에 사과하겠습니다 만, 저는이 초짜 물건에 아주 초심자입니다!

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. 

'이것은'감사에 삽입 '명령 인 것처럼 보입니다. 그러나 나는 무엇을 여기에서해야하는지 전혀 모른다!

도움을 주시면 감사하겠습니다. 미리 감사드립니다.

+0

'Transactions' 테이블에'Create' 및'Delete' 날짜가없는 경우 insert에'DeleteDate'에 null을 추가해야합니다.그럴 경우 * 열 목록을 *로 바꾸십시오. 이제는 하나의 열을 더 삽입하려고하기 때문입니다. –

답변

2

INSERT은 오히려 저조한 것으로 기록됩니다. ,

값리스트의 값 테이블 각 열에 대한 값이없는 테이블 또는 의 열 같은 순서에없는 경우 : VALUES() 소자의 경우, 다음 상태 column_list은 각 수신 값을 저장하는 열을 명시 적으로 지정하는 데 사용해야합니다. 당신이 정확하게 모든 열이 일치하지 않는 것을 -

그러나,이 같은 제약 조건에 상관없이 값/행의 근원이 무엇인지, 적용되지 않습니다 나의 이해와 믿음 (강조 추가) 테이블의 경우 column_list을 제공해야합니다. 방금 의 테이블의 모든 열 값을 제공하는 경우

자, 예를 들어, 쉽게 할 수있다 : 이제

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

을, 내 다른 관찰은이 모든 조건 제어에 대한 일 처리 때문이다 흐름은 오히려 무의미합니다. 0 행의 insert은 아무 효과가 없으며, 을 빈 테이블에 사용하면 update도 마찬가지입니다. 그래서 난 그냥 거라고 :

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 
+0

빠른 응답과 환상적인 도움을 주신 것에 대해 대단히 감사합니다. 이제 쿼리가 올바르게 실행되고 트리거가 초기화되어 정확히 예상대로 수행됩니다. 다시 한번, 많은 감사합니다! –

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

귀하의 "감사"표는 (삭제 된 날짜)을 삽입하는 것보다 더 많은 열이 있습니다; 모든 열의 이름을 지정하거나 널 (NULL)을 삽입해야합니다.

스타일을 지정하면 열 이름을 지정하면 더 좋습니다. 새로운 열을 추가 할 때 버그를 피할 수 있습니다.

+0

감사합니다.이 대회를 알지 못해서 알아내는 것이 좋습니다! –

관련 문제