Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8234399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:27:42+00:00 2026-06-07T18:27:42+00:00

I created a trigger which creates a new row with a new ID after

  • 0

I created a trigger which creates a new row with a new ID after a change. When I use the trigger with one table it works but when I use it on a table which has reference to another table, I am getting an error:

DELETE statement conflicted with the REFERENCE constraint.

The tables look like this:

Table1:

ID       BEZEICHNUNG        MENGENEINHEIT        PREIS  .....
1        Harry Potter       Book                 20
2        iPod               Music                150

Table2:

DIENSTLEISTUNG_ID      RAUM_ID 
1                      2
2                      1  

Table 3:

ID      Raumname ....
1       Elbe
2       Main

My trigger looks like this:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[DIENSTLEISTUNG_Update]
   ON [dbo].[DIENSTLEISTUNG]
   AFTER  UPDATE
AS
BEGIN
 SET  NOCOUNT ON;

    DECLARE @MAX_ID INT;
    SELECT @MAX_ID=MAX(ID) FROM [DIENSTLEISTUNG];

    declare @tmp Table(ID  numeric, BEZEICHNUNG nvarchar(64), MENGENEINHEIT nvarchar(64), 
    PREIS numeric(19,5), BESCHREIBUNG nvarchar(64), VORLAUFZEIT numeric(10),
    AZ_MO nvarchar(22), AZ_DI nvarchar(22),AZ_MI nvarchar(22),AZ_DO nvarchar(22),AZ_FR nvarchar(22),
    AZ_SA nvarchar(22),AZ_SO nvarchar(22),DIENSTLEISTUNGSART_ID numeric(38),
    UPDATE_USER numeric(38), UPDATE_DATE datetime, RUESTZEIT numeric(38),
    PERMISSIONS numeric (38), KONTRAKTPOSITION numeric(38),ARTIKELNUMMER nvarchar(64),
    ANZAHL numeric(10), BUCHUNGSHINWEIS nvarchar(255), SONDERWUNSCH char(1),
    FLAG bit)

    insert into @tmp
    select
    ID, BEZEICHNUNG, MENGENEINHEIT, 
    PREIS, BESCHREIBUNG, VORLAUFZEIT,
    AZ_MO, AZ_DI,AZ_MI,AZ_DO,AZ_FR,
    AZ_SA,AZ_SO,DIENSTLEISTUNGSART_ID,
    UPDATE_USER, UPDATE_DATE, RUESTZEIT,
    PERMISSIONS, KONTRAKTPOSITION,ARTIKELNUMMER,
    ANZAHL, BUCHUNGSHINWEIS, SONDERWUNSCH,
    1 [flag] from deleted;


    delete T from DIENSTLEISTUNG T JOIN @tmp I
    ON T.ID=I.ID

SET IDENTITY_INSERT   [DIENSTLEISTUNG] ON
INSERT INTO [DIENSTLEISTUNG] (ID, BEZEICHNUNG, MENGENEINHEIT, 
    PREIS, BESCHREIBUNG, VORLAUFZEIT,
    AZ_MO, AZ_DI,AZ_MI,AZ_DO,AZ_FR,
    AZ_SA,AZ_SO,DIENSTLEISTUNGSART_ID,
    UPDATE_USER, UPDATE_DATE, RUESTZEIT,
    PERMISSIONS, KONTRAKTPOSITION,ARTIKELNUMMER,
    ANZAHL, BUCHUNGSHINWEIS, SONDERWUNSCH,
    FLAG)
SELECT @MAX_ID+ROW_NUMBER() OVER(ORDER BY ID) [ID],BEZEICHNUNG, MENGENEINHEIT, 
    PREIS, BESCHREIBUNG, VORLAUFZEIT,
    AZ_MO, AZ_DI,AZ_MI,AZ_DO,AZ_FR,
    AZ_SA,AZ_SO,DIENSTLEISTUNGSART_ID,
    UPDATE_USER,GETDATE(),RUESTZEIT,
    PERMISSIONS, KONTRAKTPOSITION,ARTIKELNUMMER,
    ANZAHL, BUCHUNGSHINWEIS, SONDERWUNSCH,
    0 
FROM INSERTED
union all
select * from @tmp

 SET IDENTITY_INSERT dbo.DIENSTLEISTUNG OFF
 SET  NOCOUNT OFF;
END;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:27:44+00:00Added an answer on June 7, 2026 at 6:27 pm

    The reason this is occurring is because although you are re-inserting a row with the same ID, at the time when you are deleting the row SQL has no way of knowing you are going to re-insert it to maintain the referential integrity.

    It appears the purpose of this trigger is to not allow updates, and insert a new row whenever a change is made, so I think an INSTEAD OF trigger will be more suited to your needs. You can then just ignore any changes made to the existing row, and insert the new row, avoiding any deletions so avoiding any referencial integrity problems.

    ALTER TRIGGER [dbo].[DIENSTLEISTUNG_Update]
       ON [dbo].[DIENSTLEISTUNG]
       INSTEAD OF UPDATE
    AS
    BEGIN
        INSERT INTO [DIENSTLEISTUNG] (BEZEICHNUNG, MENGENEINHEIT, 
            PREIS, BESCHREIBUNG, VORLAUFZEIT,
            AZ_MO, AZ_DI,AZ_MI,AZ_DO,AZ_FR,
            AZ_SA,AZ_SO,DIENSTLEISTUNGSART_ID,
            UPDATE_USER, UPDATE_DATE, RUESTZEIT,
            PERMISSIONS, KONTRAKTPOSITION,ARTIKELNUMMER,
            ANZAHL, BUCHUNGSHINWEIS, SONDERWUNSCH,
            FLAG)
        SELECT  BEZEICHNUNG, MENGENEINHEIT, 
            PREIS, BESCHREIBUNG, VORLAUFZEIT,
            AZ_MO, AZ_DI,AZ_MI,AZ_DO,AZ_FR,
            AZ_SA,AZ_SO,DIENSTLEISTUNGSART_ID,
            UPDATE_USER,GETDATE(),RUESTZEIT,
            PERMISSIONS, KONTRAKTPOSITION,ARTIKELNUMMER,
            ANZAHL, BUCHUNGSHINWEIS, SONDERWUNSCH,
            0 
        FROM INSERTED
    
        UPDATE  DIENSTLEISTUNG
        SET     Flag = 1 
        FROM    DIENSTLEISTUNG 
                INNER JOIN INSERTED
                    ON INSERTED.ID = DIENSTLEISTUNG.ID
    
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use a trigger on a table which will be fired
hi i have created a trigger which i want to update a table field
Hello i created a SQL Trigger which copies the edited row and set a
I created a trigger which throws an error whenever new entry is stored. Here
I have created a trigger on a table which holds an audit log. When
I created a trigger on a table for updates. If any update happens, I
I have created a trigger in the xaml and it works fine. However, when
We have created a table with a trigger that updates a ModifiedDate field in
I have a table where I created an INSTEAD OF trigger to enforce some
I'm trying to create DB2 trigger which is using the procedure VOTES_COUNT but when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.