简体   繁体   中英

SQL Server 2008 R2 trigger identity column

I need to create a trigger to add a record to second table.

  • First table : tab_esami
  • Second table : tab_valutazioni

I create this trigger :

CREATE TRIGGER [dbo].[NewTrigger]
ON [dbo].[tab_esame]
AFTER INSERT
AS
BEGIN
    /* Trigger action goes here. */
    SET IDENTITY_INSERT TAB_VALUTAZIONI ON

    INSERT INTO tab_valutazioni(tab_utenti_id, nume_colesterolo,
                                nume_hdl, nume_ldl, nume_trigliceridi,
                                nume_glicemia, data_insert)
        SELECT 
            id_paziente, colesterolo_totale,
            hdl, ldl, trigliceridi,
            glicemia, data_esame
        FROM 
            INSERTED
END
GO

But when I try to insert a record into tab_esami I get an error :

Error_ON_insert

Can you help me?

The id column for tab_valutazioni is 'ID' and it is a primary key with autoincrement properties..

Thanks

If you want this code to work properly you should set Identity Column. See this. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM