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 6023441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:00:21+00:00 2026-05-23T04:00:21+00:00

I created a trigger for a table (Person) so that each time a new

  • 0

I created a trigger for a table (Person) so that each time a new Person entry is created its ID is inserted in another table (Person_ID). Person_ID table has only 2 rows: ID (primary key, int and Identity) and Person_ID (GUID whose values is passed with the trigger).
This schema cannot be changed due to other dependencies with our business logic.

Now I need to update a field of Person table with the Person_ID.ID (the identity automatically generated once that person has been created). To do that I created a trigger for Person_ID so that once a new entry is created, the generated ID will updated the target field in Person table:

UPDATE Person
SET target_Person = (SELECT JPerson_ID.ID FROM inserted)
WHERE Person.ID IN (SELECT JPerson_ID.PersonID FROM inserted)

When I create a new Person I get the following exception:

Violation of PRIMARY KEY constraint 'TBL_PERSON_A_PK'. 
Cannot insert duplicate key in object 'dbo.TBL_PERSON_A.

There is an update trigger associated to table Person that comes from a legacy component and I cannot edit or see it (it is encrypted). It seems the reason of the exception above.

Why do I get this exception even if I simply make an UPDATE and not an insert?

To solve this I disable such a trigger before executing the update and then enabling it again and it works like a charm:

EXECUTE sp_executesql N'DISABLE TRIGGER dbo.TBL_PERSON_TRU1 ON PERSON'
...
EXECUTE sp_executesql N'ENABLE TRIGGER dbo.TBL_PERSON_TRU1 ON PERSON'

However how can I be sure that this will not bring to logic errors?
Thanks.

  • 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-05-23T04:00:22+00:00Added an answer on May 23, 2026 at 4:00 am

    I don’t know if this causes your problem, but have you kept in mind that your (SELECT JPerson_ID.ID FROM inserted) could return more than one row?

    So you insert trigger must be changed to somewhat like:

    CREATE TRIGGER [dbo].[trgJPerson_ID] ON [dbo].[TBL_PERSON_A]
    FOR INSERT
    AS
    
    INSERT INTO dbo.JPerson_ID(ID )
        SELECT ID FROM inserted
    

    and accordingly the delete-trigger(if you have one)

    CREATE TRIGGER [dbo].[trgJPerson_ID] ON [dbo].[TBL_PERSON_A]
    FOR DELETE
    AS
    
    DELETE FROM dbo.JPerson_ID
        WHERE ID IN(SELECT ID FROM DELETED)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have created a table with a trigger that updates a ModifiedDate field in
I created a simple trigger on Albums table. But that trigger is not visible
I have table called Table A and I have created a trigger that handles
I would like to write trigger that after inserting record to table Person I
I created a trigger on a table for updates. If any update happens, I
I have a table where I created an INSTEAD OF trigger to enforce some
How could i create a trigger that at any insertion on my table [users]
Is there any way to automatically create a trigger on creation of new table
In Microsoft SQL Server : I've added an insert trigger my table ACCOUNTS that
I've created a logon trigger in MS SQL that needs to check dm_exec_sessions for

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.