Triggers 如何在插入数据时触发自动更新?

Triggers 如何在插入数据时触发自动更新?,triggers,ssis,Triggers,Ssis,我正在使用SSIS 2008,并试图在插入时更新表中的一列。这一列是一个uniqueidentifier字段,我还编写了一个用于更新此字段的触发器。该代码如下: CREATE TABLE dbo.rd_information3_cleaned ( c1 uniqueidentifier NULL, c2 nvarchar(50), c3 nvarchar(50), c4 nvarchar(50) ) create trigger dbo.trg_client_informat

我正在使用SSIS 2008,并试图在插入时更新表中的一列。这一列是一个uniqueidentifier字段,我还编写了一个用于更新此字段的触发器。该代码如下:

CREATE TABLE dbo.rd_information3_cleaned (
c1 uniqueidentifier NULL,
    c2 nvarchar(50),
    c3 nvarchar(50),
 c4 nvarchar(50)
)

create trigger dbo.trg_client_information_id
on client_information
after insert
as
begin
    update client_information
    set client_information_id = newid()
      from Inserted
end
我知道这段代码是有效的,因为我在SSMS中测试了它,它确实更新了本专栏。另外,我的桌子看起来像:

c1                               c2   c3   c4
xxxx-xxxx-xxxx-xxxx   A    BB  C5
xxxx-xxxx-xxxx-xxxx   A2   BB  C
xxxx-xxxx-xxxx-xxxx   A3   BB  C7
xxxx-xxxx-xxxx-xxxx   A4   BB  C
但是当我尝试运行这个SSIS包时,我只写c2-c4,因为触发器应该更新列“c1”。但是,我得到了一个错误:

Information: 0x40043007 at Write to Client_Information, SSIS.Pipeline: Pre-Execute phase is beginning.
Information: 0x4004300C at Write to Client_Information, SSIS.Pipeline: Execute phase is beginning.
Error: 0xC0202009 at Write to Client_Information, Client_Information [27]: SSIS Error Code DTS_E_OLEDBERROR.  An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available.  Source: "Microsoft SQL Server Native Client 10.0"  Hresult: 0x80004005  Description: "The statement has been terminated.".
An OLE DB record is available.  Source: "Microsoft SQL Server Native Client 10.0"  Hresult: 0x80004005  Description: "Cannot insert duplicate key row in object 'dbo.Client_Information' with unique index 'IX_Client_Demographics_Unique'.".
Error: 0xC0209029 at Write to Client_Information, Client_Information [27]: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR.  The "input "OLE DB Destination Input" (40)" failed because error code 0xC020907B occurred, and the error row disposition on "input "OLE DB Destination Input" (40)" specifies failure on error. An error occurred on the specified object of the specified component.  There may be error messages posted before this with more information about the failure.
Error: 0xC0047022 at Write to Client_Information, SSIS.Pipeline: SSIS Error Code DTS_E_PROCESSINPUTFAILED.  The ProcessInput method on component "Client_Information" (27) failed with error code 0xC0209029 while processing input "OLE DB Destination Input" (40). The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running.  There may be error messages posted before this with more information about the failure.
Information: 0x40043008 at Write to Client_Information, SSIS.Pipeline: Post Execute phase is beginning.
Information: 0x402090DF at Write to Client_Information, Client_Information [27]: The final commit for the data insertion in "component "Client_Information" (27)" has started.
Information: 0x402090E0 at Write to Client_Information, Client_Information [27]: The final commit for the data insertion  in "component "Client_Information" (27)" has ended.
Information: 0x4004300B at Write to Client_Information, SSIS.Pipeline: "component "Client_Information" (27)" wrote 2 rows.
Information: 0x40043009 at Write to Client_Information, SSIS.Pipeline: Cleanup phase is beginning.
Task failed: Write to Client_Information
SSIS package "Echo Information Migration2.dtsx" finished: Success.
The program '[2564] Echo Information Migration2.dtsx: DTS' has exited with code 0 (0x0).
我几乎可以肯定这个错误的原因是客户端信息id字段。因为我能够在SSMS中写入多行,如果我只是使该字段成为唯一标识符;否则,我无法在此表中写入多行。所以我想知道。我是否可能需要在SSIS中设置一个TIME属性,以便为触发器提供足够的工作时间?否则我为什么会犯这个错误


此外,我编辑了OLE DB目标,并将最大插入提交大小设置为1,但仍然出现相同的错误

长话短说,不要使用触发器,只需使用默认值,如中所示。您发布的表DDL和触发器代码似乎互不相关,但我认为您确实需要这样的代码:

create table dbo.Clients (
   ClientID uniqueidentifier not null primary key default newid(),
   ClientAttributeA nvarchar(50) not null,
   -- etc.
)
我怀疑,当您在SSMS中测试时,您测试了一次插入一行,但您的SSIS包正在插入多行。NEWID()函数在触发器中只调用一次,因此如果插入一行,它将起作用,但如果插入多行,则每个行都会得到相同的NEWID()值,从而导致重复密钥冲突