How to get the ignored row while IGNORE_DUP_KEY is ON on SQL Server?












6















I have a tmp table shown below.



enter image description here



In the above table





  • IGNORE_DUP_KEY is set to ON


and




  • the id column is the primary key.


Said table has only one key. After inserting lots of data I will get the




Duplicate key was ignored.




message because of some redundant data.



I want to check which redundant row was tried to insert. I checked the origin of the message it was sys.messages.



Now, how to store the row which failed while the insertion attempt took place because of the duplicate primary key value?










share|improve this question









New contributor




V T Vishwanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    6















    I have a tmp table shown below.



    enter image description here



    In the above table





    • IGNORE_DUP_KEY is set to ON


    and




    • the id column is the primary key.


    Said table has only one key. After inserting lots of data I will get the




    Duplicate key was ignored.




    message because of some redundant data.



    I want to check which redundant row was tried to insert. I checked the origin of the message it was sys.messages.



    Now, how to store the row which failed while the insertion attempt took place because of the duplicate primary key value?










    share|improve this question









    New contributor




    V T Vishwanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      6












      6








      6


      1






      I have a tmp table shown below.



      enter image description here



      In the above table





      • IGNORE_DUP_KEY is set to ON


      and




      • the id column is the primary key.


      Said table has only one key. After inserting lots of data I will get the




      Duplicate key was ignored.




      message because of some redundant data.



      I want to check which redundant row was tried to insert. I checked the origin of the message it was sys.messages.



      Now, how to store the row which failed while the insertion attempt took place because of the duplicate primary key value?










      share|improve this question









      New contributor




      V T Vishwanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I have a tmp table shown below.



      enter image description here



      In the above table





      • IGNORE_DUP_KEY is set to ON


      and




      • the id column is the primary key.


      Said table has only one key. After inserting lots of data I will get the




      Duplicate key was ignored.




      message because of some redundant data.



      I want to check which redundant row was tried to insert. I checked the origin of the message it was sys.messages.



      Now, how to store the row which failed while the insertion attempt took place because of the duplicate primary key value?







      sql-server primary-key






      share|improve this question









      New contributor




      V T Vishwanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      V T Vishwanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 10 hours ago









      MDCCL

      6,69731744




      6,69731744






      New contributor




      V T Vishwanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 17 hours ago









      V T VishwanathV T Vishwanath

      1334




      1334




      New contributor




      V T Vishwanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      V T Vishwanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      V T Vishwanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes


















          4














          You could capture all the PK exceptions using a trace or you could capture which sql statements trigger the duplicate key message with an extended event.



          In the following examples it shows you how to either capture the 'Exception' with a profiler trace, or capture the sql text executed that triggers the duplicate key message using an extended event.



          The difference being that the 'exception' trace gets a row for each violation, which we could log to a file, and then read from that file.



          What could you capture?



          Before the user error message is returned,a PK violation occurs internally:



          enter image description here



          Which in turns gives the duplicate key value:



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          While the user does not see this message, we could either capture these with a trace or an extended event.



          enter image description here





          Profiler trace on the exceptions on the table



          enter image description here



          Add a filter



          enter image description here



          Capture the PK violations, even when IGNORE_DUP_KEY = ON



          enter image description here



          Error message captured



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          The problem here is that it can get messy real fast, as it gives a record per Failed value, so if 1 and 2 already exist



          INSERT INTO ignore_dup_key(a) VALUES(1), (2)


          It gives two new exceptions in the profiler trace:



          1)



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          2)



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (2).


          Saving it to a table



          enter image description here



          Select from the new table



          SELECT *
          FROM [my_test].[dbo].[FindViolations];


          enter image description here



          So, when Inserting 1000 Duplicate ID's, the logging will hold 1000 extra records



            TRUNCATE TABLE [my_test].[dbo].[FindViolations];

          INSERT INTO ignore_dup_key(a)
          select a from ignore_dup_key; -- 1000 duplicate records

          SELECT COUNT(*) from [my_test].[dbo].[FindViolations];


          Result



          (No column name)
          1000


          enter image description here
          ETC.





          Create the extended event



          enter image description here



          Do not choose a template



          enter image description here



          Select the error_reported event



          enter image description here



          Select the SQL_TEXT and username, and any additional things you would want to capture



          enter image description here



          Result



          enter image description here



          You could also add a filter, as to filter out non-duplicate key errors
          enter image description here






          share|improve this answer


























          • Can we capture for a specific database and table?

            – V T Vishwanath
            15 hours ago











          • In the profiler trace window you can add additional filters, this is located at events selection --> Column Filters... . There you can add a databasename, and the table filter needs to be added to the TextData filter

            – Randi Vertongen
            15 hours ago













          • In the case of Extended Events. What if we have bulk insertion ? or using BCP? How to get the exact record which failed. I tried with multiple insert statements and it gives all sql text. Can't find the exact insert statement which failed.

            – V T Vishwanath
            14 hours ago













          • @VTVishwanath For the moment being I have not found the correct event for capturing all the different failed records, only the statement executed. For now, only by using the profiler trace and logging it to a table was I able to get all the records that failed through T-SQL.

            – Randi Vertongen
            14 hours ago





















          5














          There would be additional overhead, but one option might be to create an instead of insert trigger which would check for duplicates first and route those to another table.



          --demo setup
          set nocount on
          DROP TABLE IF EXISTS [dbo].[TestTable]
          CREATE TABLE [dbo].[TestTable](
          [ID] [int] NOT NULL,
          [ExtraInformation] [varchar](50) NOT NULL,
          CONSTRAINT [PK_Employee_ID] PRIMARY KEY CLUSTERED
          (
          [ID] ASC
          ) with (IGNORE_DUP_KEY = ON)
          ) ON [PRIMARY]
          GO

          DROP TABLE IF EXISTS [dbo].[TestTableIgnoredDups]
          CREATE TABLE [dbo].[TestTableIgnoredDups](
          [ID] [int] NOT NULL,
          [ExtraInformation] [varchar](50) NOT NULL
          )
          ON [PRIMARY]
          GO

          --create INSTEAD OF trigger
          CREATE TRIGGER TestTable_InsteadOfInsert ON dbo.TestTable
          INSTEAD OF INSERT
          AS
          BEGIN
          --select rows to be inserted into #temp
          SELECT *
          INTO #temp
          FROM inserted

          --insert rows to TestTableIgnoredDups where primary key already exists
          INSERT INTO TestTableIgnoredDups
          SELECT t.*
          FROM #temp t
          JOIN TestTable tt
          ON tt.id = t.id

          --delete the duplicate rows from #temp
          DELETE t
          FROM #temp t
          JOIN TestTable tt
          ON tt.id = t.id

          --insert rows to TestTableIgnoredDups where duplicates
          --exist on the inserted virtual table, but not necessarily on TestTable
          ;WITH DupsOnInserted
          AS (
          SELECT id
          ,count(*) AS cnt
          FROM #temp
          GROUP BY id
          HAVING count(*) > 1
          )
          INSERT INTO TestTableIgnoredDups
          SELECT t.*
          FROM #temp t
          JOIN DupsOnInserted doi
          ON doi.id = t.id;

          ;WITH DupsOnInserted
          AS (
          SELECT id
          ,count(*) AS cnt
          FROM #temp
          GROUP BY id
          HAVING count(*) > 1
          )
          DELETE t
          FROM #temp t
          JOIN DupsOnInserted doi
          ON doi.id = t.ID

          --insert the remaining rows to TestTable
          INSERT INTO TestTable
          SELECT *
          FROM #temp
          END
          GO




          --verify by trying to insert a duplicate row
          insert into testtable(id,ExtraInformation) values(1,'RowOne')
          insert into testtable(id,ExtraInformation) values(1,'RowOneDup')

          select * from TestTable
          select * from TestTableIgnoredDups




          Data from TestTable



          | ID | ExtraInformation |
          |----|------------------|
          | 1 | RowOne |


          Data from TestTableIgnoreDups



          | ID | ExtraInformation |
          |----|------------------|
          | 1 | RowOneDup |





          share|improve this answer


























          • Can we use any system table to get the info? Because trigger will make server slow if we have large number of rows to be inserted.

            – V T Vishwanath
            16 hours ago











          • @VTVishwanath - the only way I know to satisfy your requirement of (Now how to store the record which is failed while insertion because of duplicate primary key.) is with an instead of trigger.

            – Scott Hodgin
            15 hours ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          V T Vishwanath is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f227267%2fhow-to-get-the-ignored-row-while-ignore-dup-key-is-on-on-sql-server%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          You could capture all the PK exceptions using a trace or you could capture which sql statements trigger the duplicate key message with an extended event.



          In the following examples it shows you how to either capture the 'Exception' with a profiler trace, or capture the sql text executed that triggers the duplicate key message using an extended event.



          The difference being that the 'exception' trace gets a row for each violation, which we could log to a file, and then read from that file.



          What could you capture?



          Before the user error message is returned,a PK violation occurs internally:



          enter image description here



          Which in turns gives the duplicate key value:



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          While the user does not see this message, we could either capture these with a trace or an extended event.



          enter image description here





          Profiler trace on the exceptions on the table



          enter image description here



          Add a filter



          enter image description here



          Capture the PK violations, even when IGNORE_DUP_KEY = ON



          enter image description here



          Error message captured



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          The problem here is that it can get messy real fast, as it gives a record per Failed value, so if 1 and 2 already exist



          INSERT INTO ignore_dup_key(a) VALUES(1), (2)


          It gives two new exceptions in the profiler trace:



          1)



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          2)



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (2).


          Saving it to a table



          enter image description here



          Select from the new table



          SELECT *
          FROM [my_test].[dbo].[FindViolations];


          enter image description here



          So, when Inserting 1000 Duplicate ID's, the logging will hold 1000 extra records



            TRUNCATE TABLE [my_test].[dbo].[FindViolations];

          INSERT INTO ignore_dup_key(a)
          select a from ignore_dup_key; -- 1000 duplicate records

          SELECT COUNT(*) from [my_test].[dbo].[FindViolations];


          Result



          (No column name)
          1000


          enter image description here
          ETC.





          Create the extended event



          enter image description here



          Do not choose a template



          enter image description here



          Select the error_reported event



          enter image description here



          Select the SQL_TEXT and username, and any additional things you would want to capture



          enter image description here



          Result



          enter image description here



          You could also add a filter, as to filter out non-duplicate key errors
          enter image description here






          share|improve this answer


























          • Can we capture for a specific database and table?

            – V T Vishwanath
            15 hours ago











          • In the profiler trace window you can add additional filters, this is located at events selection --> Column Filters... . There you can add a databasename, and the table filter needs to be added to the TextData filter

            – Randi Vertongen
            15 hours ago













          • In the case of Extended Events. What if we have bulk insertion ? or using BCP? How to get the exact record which failed. I tried with multiple insert statements and it gives all sql text. Can't find the exact insert statement which failed.

            – V T Vishwanath
            14 hours ago













          • @VTVishwanath For the moment being I have not found the correct event for capturing all the different failed records, only the statement executed. For now, only by using the profiler trace and logging it to a table was I able to get all the records that failed through T-SQL.

            – Randi Vertongen
            14 hours ago


















          4














          You could capture all the PK exceptions using a trace or you could capture which sql statements trigger the duplicate key message with an extended event.



          In the following examples it shows you how to either capture the 'Exception' with a profiler trace, or capture the sql text executed that triggers the duplicate key message using an extended event.



          The difference being that the 'exception' trace gets a row for each violation, which we could log to a file, and then read from that file.



          What could you capture?



          Before the user error message is returned,a PK violation occurs internally:



          enter image description here



          Which in turns gives the duplicate key value:



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          While the user does not see this message, we could either capture these with a trace or an extended event.



          enter image description here





          Profiler trace on the exceptions on the table



          enter image description here



          Add a filter



          enter image description here



          Capture the PK violations, even when IGNORE_DUP_KEY = ON



          enter image description here



          Error message captured



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          The problem here is that it can get messy real fast, as it gives a record per Failed value, so if 1 and 2 already exist



          INSERT INTO ignore_dup_key(a) VALUES(1), (2)


          It gives two new exceptions in the profiler trace:



          1)



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          2)



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (2).


          Saving it to a table



          enter image description here



          Select from the new table



          SELECT *
          FROM [my_test].[dbo].[FindViolations];


          enter image description here



          So, when Inserting 1000 Duplicate ID's, the logging will hold 1000 extra records



            TRUNCATE TABLE [my_test].[dbo].[FindViolations];

          INSERT INTO ignore_dup_key(a)
          select a from ignore_dup_key; -- 1000 duplicate records

          SELECT COUNT(*) from [my_test].[dbo].[FindViolations];


          Result



          (No column name)
          1000


          enter image description here
          ETC.





          Create the extended event



          enter image description here



          Do not choose a template



          enter image description here



          Select the error_reported event



          enter image description here



          Select the SQL_TEXT and username, and any additional things you would want to capture



          enter image description here



          Result



          enter image description here



          You could also add a filter, as to filter out non-duplicate key errors
          enter image description here






          share|improve this answer


























          • Can we capture for a specific database and table?

            – V T Vishwanath
            15 hours ago











          • In the profiler trace window you can add additional filters, this is located at events selection --> Column Filters... . There you can add a databasename, and the table filter needs to be added to the TextData filter

            – Randi Vertongen
            15 hours ago













          • In the case of Extended Events. What if we have bulk insertion ? or using BCP? How to get the exact record which failed. I tried with multiple insert statements and it gives all sql text. Can't find the exact insert statement which failed.

            – V T Vishwanath
            14 hours ago













          • @VTVishwanath For the moment being I have not found the correct event for capturing all the different failed records, only the statement executed. For now, only by using the profiler trace and logging it to a table was I able to get all the records that failed through T-SQL.

            – Randi Vertongen
            14 hours ago
















          4












          4








          4







          You could capture all the PK exceptions using a trace or you could capture which sql statements trigger the duplicate key message with an extended event.



          In the following examples it shows you how to either capture the 'Exception' with a profiler trace, or capture the sql text executed that triggers the duplicate key message using an extended event.



          The difference being that the 'exception' trace gets a row for each violation, which we could log to a file, and then read from that file.



          What could you capture?



          Before the user error message is returned,a PK violation occurs internally:



          enter image description here



          Which in turns gives the duplicate key value:



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          While the user does not see this message, we could either capture these with a trace or an extended event.



          enter image description here





          Profiler trace on the exceptions on the table



          enter image description here



          Add a filter



          enter image description here



          Capture the PK violations, even when IGNORE_DUP_KEY = ON



          enter image description here



          Error message captured



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          The problem here is that it can get messy real fast, as it gives a record per Failed value, so if 1 and 2 already exist



          INSERT INTO ignore_dup_key(a) VALUES(1), (2)


          It gives two new exceptions in the profiler trace:



          1)



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          2)



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (2).


          Saving it to a table



          enter image description here



          Select from the new table



          SELECT *
          FROM [my_test].[dbo].[FindViolations];


          enter image description here



          So, when Inserting 1000 Duplicate ID's, the logging will hold 1000 extra records



            TRUNCATE TABLE [my_test].[dbo].[FindViolations];

          INSERT INTO ignore_dup_key(a)
          select a from ignore_dup_key; -- 1000 duplicate records

          SELECT COUNT(*) from [my_test].[dbo].[FindViolations];


          Result



          (No column name)
          1000


          enter image description here
          ETC.





          Create the extended event



          enter image description here



          Do not choose a template



          enter image description here



          Select the error_reported event



          enter image description here



          Select the SQL_TEXT and username, and any additional things you would want to capture



          enter image description here



          Result



          enter image description here



          You could also add a filter, as to filter out non-duplicate key errors
          enter image description here






          share|improve this answer















          You could capture all the PK exceptions using a trace or you could capture which sql statements trigger the duplicate key message with an extended event.



          In the following examples it shows you how to either capture the 'Exception' with a profiler trace, or capture the sql text executed that triggers the duplicate key message using an extended event.



          The difference being that the 'exception' trace gets a row for each violation, which we could log to a file, and then read from that file.



          What could you capture?



          Before the user error message is returned,a PK violation occurs internally:



          enter image description here



          Which in turns gives the duplicate key value:



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          While the user does not see this message, we could either capture these with a trace or an extended event.



          enter image description here





          Profiler trace on the exceptions on the table



          enter image description here



          Add a filter



          enter image description here



          Capture the PK violations, even when IGNORE_DUP_KEY = ON



          enter image description here



          Error message captured



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          The problem here is that it can get messy real fast, as it gives a record per Failed value, so if 1 and 2 already exist



          INSERT INTO ignore_dup_key(a) VALUES(1), (2)


          It gives two new exceptions in the profiler trace:



          1)



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (1).


          2)



          Violation of PRIMARY KEY constraint 'PK__ignore_d__3BD0198E9F9BACEA'. Cannot insert duplicate key in object 'dbo.ignore_dup_key'. The duplicate key value is (2).


          Saving it to a table



          enter image description here



          Select from the new table



          SELECT *
          FROM [my_test].[dbo].[FindViolations];


          enter image description here



          So, when Inserting 1000 Duplicate ID's, the logging will hold 1000 extra records



            TRUNCATE TABLE [my_test].[dbo].[FindViolations];

          INSERT INTO ignore_dup_key(a)
          select a from ignore_dup_key; -- 1000 duplicate records

          SELECT COUNT(*) from [my_test].[dbo].[FindViolations];


          Result



          (No column name)
          1000


          enter image description here
          ETC.





          Create the extended event



          enter image description here



          Do not choose a template



          enter image description here



          Select the error_reported event



          enter image description here



          Select the SQL_TEXT and username, and any additional things you would want to capture



          enter image description here



          Result



          enter image description here



          You could also add a filter, as to filter out non-duplicate key errors
          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 14 hours ago

























          answered 16 hours ago









          Randi VertongenRandi Vertongen

          1,725315




          1,725315













          • Can we capture for a specific database and table?

            – V T Vishwanath
            15 hours ago











          • In the profiler trace window you can add additional filters, this is located at events selection --> Column Filters... . There you can add a databasename, and the table filter needs to be added to the TextData filter

            – Randi Vertongen
            15 hours ago













          • In the case of Extended Events. What if we have bulk insertion ? or using BCP? How to get the exact record which failed. I tried with multiple insert statements and it gives all sql text. Can't find the exact insert statement which failed.

            – V T Vishwanath
            14 hours ago













          • @VTVishwanath For the moment being I have not found the correct event for capturing all the different failed records, only the statement executed. For now, only by using the profiler trace and logging it to a table was I able to get all the records that failed through T-SQL.

            – Randi Vertongen
            14 hours ago





















          • Can we capture for a specific database and table?

            – V T Vishwanath
            15 hours ago











          • In the profiler trace window you can add additional filters, this is located at events selection --> Column Filters... . There you can add a databasename, and the table filter needs to be added to the TextData filter

            – Randi Vertongen
            15 hours ago













          • In the case of Extended Events. What if we have bulk insertion ? or using BCP? How to get the exact record which failed. I tried with multiple insert statements and it gives all sql text. Can't find the exact insert statement which failed.

            – V T Vishwanath
            14 hours ago













          • @VTVishwanath For the moment being I have not found the correct event for capturing all the different failed records, only the statement executed. For now, only by using the profiler trace and logging it to a table was I able to get all the records that failed through T-SQL.

            – Randi Vertongen
            14 hours ago



















          Can we capture for a specific database and table?

          – V T Vishwanath
          15 hours ago





          Can we capture for a specific database and table?

          – V T Vishwanath
          15 hours ago













          In the profiler trace window you can add additional filters, this is located at events selection --> Column Filters... . There you can add a databasename, and the table filter needs to be added to the TextData filter

          – Randi Vertongen
          15 hours ago







          In the profiler trace window you can add additional filters, this is located at events selection --> Column Filters... . There you can add a databasename, and the table filter needs to be added to the TextData filter

          – Randi Vertongen
          15 hours ago















          In the case of Extended Events. What if we have bulk insertion ? or using BCP? How to get the exact record which failed. I tried with multiple insert statements and it gives all sql text. Can't find the exact insert statement which failed.

          – V T Vishwanath
          14 hours ago







          In the case of Extended Events. What if we have bulk insertion ? or using BCP? How to get the exact record which failed. I tried with multiple insert statements and it gives all sql text. Can't find the exact insert statement which failed.

          – V T Vishwanath
          14 hours ago















          @VTVishwanath For the moment being I have not found the correct event for capturing all the different failed records, only the statement executed. For now, only by using the profiler trace and logging it to a table was I able to get all the records that failed through T-SQL.

          – Randi Vertongen
          14 hours ago







          @VTVishwanath For the moment being I have not found the correct event for capturing all the different failed records, only the statement executed. For now, only by using the profiler trace and logging it to a table was I able to get all the records that failed through T-SQL.

          – Randi Vertongen
          14 hours ago















          5














          There would be additional overhead, but one option might be to create an instead of insert trigger which would check for duplicates first and route those to another table.



          --demo setup
          set nocount on
          DROP TABLE IF EXISTS [dbo].[TestTable]
          CREATE TABLE [dbo].[TestTable](
          [ID] [int] NOT NULL,
          [ExtraInformation] [varchar](50) NOT NULL,
          CONSTRAINT [PK_Employee_ID] PRIMARY KEY CLUSTERED
          (
          [ID] ASC
          ) with (IGNORE_DUP_KEY = ON)
          ) ON [PRIMARY]
          GO

          DROP TABLE IF EXISTS [dbo].[TestTableIgnoredDups]
          CREATE TABLE [dbo].[TestTableIgnoredDups](
          [ID] [int] NOT NULL,
          [ExtraInformation] [varchar](50) NOT NULL
          )
          ON [PRIMARY]
          GO

          --create INSTEAD OF trigger
          CREATE TRIGGER TestTable_InsteadOfInsert ON dbo.TestTable
          INSTEAD OF INSERT
          AS
          BEGIN
          --select rows to be inserted into #temp
          SELECT *
          INTO #temp
          FROM inserted

          --insert rows to TestTableIgnoredDups where primary key already exists
          INSERT INTO TestTableIgnoredDups
          SELECT t.*
          FROM #temp t
          JOIN TestTable tt
          ON tt.id = t.id

          --delete the duplicate rows from #temp
          DELETE t
          FROM #temp t
          JOIN TestTable tt
          ON tt.id = t.id

          --insert rows to TestTableIgnoredDups where duplicates
          --exist on the inserted virtual table, but not necessarily on TestTable
          ;WITH DupsOnInserted
          AS (
          SELECT id
          ,count(*) AS cnt
          FROM #temp
          GROUP BY id
          HAVING count(*) > 1
          )
          INSERT INTO TestTableIgnoredDups
          SELECT t.*
          FROM #temp t
          JOIN DupsOnInserted doi
          ON doi.id = t.id;

          ;WITH DupsOnInserted
          AS (
          SELECT id
          ,count(*) AS cnt
          FROM #temp
          GROUP BY id
          HAVING count(*) > 1
          )
          DELETE t
          FROM #temp t
          JOIN DupsOnInserted doi
          ON doi.id = t.ID

          --insert the remaining rows to TestTable
          INSERT INTO TestTable
          SELECT *
          FROM #temp
          END
          GO




          --verify by trying to insert a duplicate row
          insert into testtable(id,ExtraInformation) values(1,'RowOne')
          insert into testtable(id,ExtraInformation) values(1,'RowOneDup')

          select * from TestTable
          select * from TestTableIgnoredDups




          Data from TestTable



          | ID | ExtraInformation |
          |----|------------------|
          | 1 | RowOne |


          Data from TestTableIgnoreDups



          | ID | ExtraInformation |
          |----|------------------|
          | 1 | RowOneDup |





          share|improve this answer


























          • Can we use any system table to get the info? Because trigger will make server slow if we have large number of rows to be inserted.

            – V T Vishwanath
            16 hours ago











          • @VTVishwanath - the only way I know to satisfy your requirement of (Now how to store the record which is failed while insertion because of duplicate primary key.) is with an instead of trigger.

            – Scott Hodgin
            15 hours ago
















          5














          There would be additional overhead, but one option might be to create an instead of insert trigger which would check for duplicates first and route those to another table.



          --demo setup
          set nocount on
          DROP TABLE IF EXISTS [dbo].[TestTable]
          CREATE TABLE [dbo].[TestTable](
          [ID] [int] NOT NULL,
          [ExtraInformation] [varchar](50) NOT NULL,
          CONSTRAINT [PK_Employee_ID] PRIMARY KEY CLUSTERED
          (
          [ID] ASC
          ) with (IGNORE_DUP_KEY = ON)
          ) ON [PRIMARY]
          GO

          DROP TABLE IF EXISTS [dbo].[TestTableIgnoredDups]
          CREATE TABLE [dbo].[TestTableIgnoredDups](
          [ID] [int] NOT NULL,
          [ExtraInformation] [varchar](50) NOT NULL
          )
          ON [PRIMARY]
          GO

          --create INSTEAD OF trigger
          CREATE TRIGGER TestTable_InsteadOfInsert ON dbo.TestTable
          INSTEAD OF INSERT
          AS
          BEGIN
          --select rows to be inserted into #temp
          SELECT *
          INTO #temp
          FROM inserted

          --insert rows to TestTableIgnoredDups where primary key already exists
          INSERT INTO TestTableIgnoredDups
          SELECT t.*
          FROM #temp t
          JOIN TestTable tt
          ON tt.id = t.id

          --delete the duplicate rows from #temp
          DELETE t
          FROM #temp t
          JOIN TestTable tt
          ON tt.id = t.id

          --insert rows to TestTableIgnoredDups where duplicates
          --exist on the inserted virtual table, but not necessarily on TestTable
          ;WITH DupsOnInserted
          AS (
          SELECT id
          ,count(*) AS cnt
          FROM #temp
          GROUP BY id
          HAVING count(*) > 1
          )
          INSERT INTO TestTableIgnoredDups
          SELECT t.*
          FROM #temp t
          JOIN DupsOnInserted doi
          ON doi.id = t.id;

          ;WITH DupsOnInserted
          AS (
          SELECT id
          ,count(*) AS cnt
          FROM #temp
          GROUP BY id
          HAVING count(*) > 1
          )
          DELETE t
          FROM #temp t
          JOIN DupsOnInserted doi
          ON doi.id = t.ID

          --insert the remaining rows to TestTable
          INSERT INTO TestTable
          SELECT *
          FROM #temp
          END
          GO




          --verify by trying to insert a duplicate row
          insert into testtable(id,ExtraInformation) values(1,'RowOne')
          insert into testtable(id,ExtraInformation) values(1,'RowOneDup')

          select * from TestTable
          select * from TestTableIgnoredDups




          Data from TestTable



          | ID | ExtraInformation |
          |----|------------------|
          | 1 | RowOne |


          Data from TestTableIgnoreDups



          | ID | ExtraInformation |
          |----|------------------|
          | 1 | RowOneDup |





          share|improve this answer


























          • Can we use any system table to get the info? Because trigger will make server slow if we have large number of rows to be inserted.

            – V T Vishwanath
            16 hours ago











          • @VTVishwanath - the only way I know to satisfy your requirement of (Now how to store the record which is failed while insertion because of duplicate primary key.) is with an instead of trigger.

            – Scott Hodgin
            15 hours ago














          5












          5








          5







          There would be additional overhead, but one option might be to create an instead of insert trigger which would check for duplicates first and route those to another table.



          --demo setup
          set nocount on
          DROP TABLE IF EXISTS [dbo].[TestTable]
          CREATE TABLE [dbo].[TestTable](
          [ID] [int] NOT NULL,
          [ExtraInformation] [varchar](50) NOT NULL,
          CONSTRAINT [PK_Employee_ID] PRIMARY KEY CLUSTERED
          (
          [ID] ASC
          ) with (IGNORE_DUP_KEY = ON)
          ) ON [PRIMARY]
          GO

          DROP TABLE IF EXISTS [dbo].[TestTableIgnoredDups]
          CREATE TABLE [dbo].[TestTableIgnoredDups](
          [ID] [int] NOT NULL,
          [ExtraInformation] [varchar](50) NOT NULL
          )
          ON [PRIMARY]
          GO

          --create INSTEAD OF trigger
          CREATE TRIGGER TestTable_InsteadOfInsert ON dbo.TestTable
          INSTEAD OF INSERT
          AS
          BEGIN
          --select rows to be inserted into #temp
          SELECT *
          INTO #temp
          FROM inserted

          --insert rows to TestTableIgnoredDups where primary key already exists
          INSERT INTO TestTableIgnoredDups
          SELECT t.*
          FROM #temp t
          JOIN TestTable tt
          ON tt.id = t.id

          --delete the duplicate rows from #temp
          DELETE t
          FROM #temp t
          JOIN TestTable tt
          ON tt.id = t.id

          --insert rows to TestTableIgnoredDups where duplicates
          --exist on the inserted virtual table, but not necessarily on TestTable
          ;WITH DupsOnInserted
          AS (
          SELECT id
          ,count(*) AS cnt
          FROM #temp
          GROUP BY id
          HAVING count(*) > 1
          )
          INSERT INTO TestTableIgnoredDups
          SELECT t.*
          FROM #temp t
          JOIN DupsOnInserted doi
          ON doi.id = t.id;

          ;WITH DupsOnInserted
          AS (
          SELECT id
          ,count(*) AS cnt
          FROM #temp
          GROUP BY id
          HAVING count(*) > 1
          )
          DELETE t
          FROM #temp t
          JOIN DupsOnInserted doi
          ON doi.id = t.ID

          --insert the remaining rows to TestTable
          INSERT INTO TestTable
          SELECT *
          FROM #temp
          END
          GO




          --verify by trying to insert a duplicate row
          insert into testtable(id,ExtraInformation) values(1,'RowOne')
          insert into testtable(id,ExtraInformation) values(1,'RowOneDup')

          select * from TestTable
          select * from TestTableIgnoredDups




          Data from TestTable



          | ID | ExtraInformation |
          |----|------------------|
          | 1 | RowOne |


          Data from TestTableIgnoreDups



          | ID | ExtraInformation |
          |----|------------------|
          | 1 | RowOneDup |





          share|improve this answer















          There would be additional overhead, but one option might be to create an instead of insert trigger which would check for duplicates first and route those to another table.



          --demo setup
          set nocount on
          DROP TABLE IF EXISTS [dbo].[TestTable]
          CREATE TABLE [dbo].[TestTable](
          [ID] [int] NOT NULL,
          [ExtraInformation] [varchar](50) NOT NULL,
          CONSTRAINT [PK_Employee_ID] PRIMARY KEY CLUSTERED
          (
          [ID] ASC
          ) with (IGNORE_DUP_KEY = ON)
          ) ON [PRIMARY]
          GO

          DROP TABLE IF EXISTS [dbo].[TestTableIgnoredDups]
          CREATE TABLE [dbo].[TestTableIgnoredDups](
          [ID] [int] NOT NULL,
          [ExtraInformation] [varchar](50) NOT NULL
          )
          ON [PRIMARY]
          GO

          --create INSTEAD OF trigger
          CREATE TRIGGER TestTable_InsteadOfInsert ON dbo.TestTable
          INSTEAD OF INSERT
          AS
          BEGIN
          --select rows to be inserted into #temp
          SELECT *
          INTO #temp
          FROM inserted

          --insert rows to TestTableIgnoredDups where primary key already exists
          INSERT INTO TestTableIgnoredDups
          SELECT t.*
          FROM #temp t
          JOIN TestTable tt
          ON tt.id = t.id

          --delete the duplicate rows from #temp
          DELETE t
          FROM #temp t
          JOIN TestTable tt
          ON tt.id = t.id

          --insert rows to TestTableIgnoredDups where duplicates
          --exist on the inserted virtual table, but not necessarily on TestTable
          ;WITH DupsOnInserted
          AS (
          SELECT id
          ,count(*) AS cnt
          FROM #temp
          GROUP BY id
          HAVING count(*) > 1
          )
          INSERT INTO TestTableIgnoredDups
          SELECT t.*
          FROM #temp t
          JOIN DupsOnInserted doi
          ON doi.id = t.id;

          ;WITH DupsOnInserted
          AS (
          SELECT id
          ,count(*) AS cnt
          FROM #temp
          GROUP BY id
          HAVING count(*) > 1
          )
          DELETE t
          FROM #temp t
          JOIN DupsOnInserted doi
          ON doi.id = t.ID

          --insert the remaining rows to TestTable
          INSERT INTO TestTable
          SELECT *
          FROM #temp
          END
          GO




          --verify by trying to insert a duplicate row
          insert into testtable(id,ExtraInformation) values(1,'RowOne')
          insert into testtable(id,ExtraInformation) values(1,'RowOneDup')

          select * from TestTable
          select * from TestTableIgnoredDups




          Data from TestTable



          | ID | ExtraInformation |
          |----|------------------|
          | 1 | RowOne |


          Data from TestTableIgnoreDups



          | ID | ExtraInformation |
          |----|------------------|
          | 1 | RowOneDup |






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 15 hours ago

























          answered 16 hours ago









          Scott HodginScott Hodgin

          17k21534




          17k21534













          • Can we use any system table to get the info? Because trigger will make server slow if we have large number of rows to be inserted.

            – V T Vishwanath
            16 hours ago











          • @VTVishwanath - the only way I know to satisfy your requirement of (Now how to store the record which is failed while insertion because of duplicate primary key.) is with an instead of trigger.

            – Scott Hodgin
            15 hours ago



















          • Can we use any system table to get the info? Because trigger will make server slow if we have large number of rows to be inserted.

            – V T Vishwanath
            16 hours ago











          • @VTVishwanath - the only way I know to satisfy your requirement of (Now how to store the record which is failed while insertion because of duplicate primary key.) is with an instead of trigger.

            – Scott Hodgin
            15 hours ago

















          Can we use any system table to get the info? Because trigger will make server slow if we have large number of rows to be inserted.

          – V T Vishwanath
          16 hours ago





          Can we use any system table to get the info? Because trigger will make server slow if we have large number of rows to be inserted.

          – V T Vishwanath
          16 hours ago













          @VTVishwanath - the only way I know to satisfy your requirement of (Now how to store the record which is failed while insertion because of duplicate primary key.) is with an instead of trigger.

          – Scott Hodgin
          15 hours ago





          @VTVishwanath - the only way I know to satisfy your requirement of (Now how to store the record which is failed while insertion because of duplicate primary key.) is with an instead of trigger.

          – Scott Hodgin
          15 hours ago










          V T Vishwanath is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          V T Vishwanath is a new contributor. Be nice, and check out our Code of Conduct.













          V T Vishwanath is a new contributor. Be nice, and check out our Code of Conduct.












          V T Vishwanath is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f227267%2fhow-to-get-the-ignored-row-while-ignore-dup-key-is-on-on-sql-server%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Сан-Квентин

          Алькесар

          Josef Freinademetz