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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:47:59+00:00 2026-06-06T19:47:59+00:00

There is a post here that talks about this as being a bad idea

  • 0

There is a post here that talks about this as being a bad idea and that instead of using a unique constraint I have to always INSERT only data not in the database. Now my table structure is as follows:

CREATE TABLE [dbo].[TEST](
    [ColA] [varchar](255) NULL,
    [ColB] [datetime] NULL,
    [ColC] [datetime] NULL,
    [ColD] [int] NOT NULL,
) ON [PRIMARY]

Only unique rows are expected so for me it makes sense in setting up a unique constraint as:

ALTER TABLE dbo.TEST
  ADD CONSTRAINT uniqueRows UNIQUE (ColA, ColB, ColC, ColD) WITH (IGNORE_DUP_KEY = ON)

When new information is being consumed, due to many reasons, there could be duplicate data being processed because my code does not maintain state and hence at least to me it makes sense to ignore any duplicate data.

However, in the linked post, @PhilipKelley in his answer, says that this is a bad idea and that one should make checks something along the lines of:

INSERT INTO X 
VALUES(Y,Z)    
WHERE Y  NOT IN (SELECT Y FROM X)

which in my case translates to:

INSERT INTO dbo.TEST 
VALUES(ValA,ValB,ValC,ValD) 
WHERE (Some complicated check)

Or perhaps make some fancy primary key. The response here hints that if I know this is indeed being used as a feature, I can go ahead and use the IGNORE_DUP_KEY option and things will be ok. What is the suggested path in my case?

  • 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-06T19:48:01+00:00Added an answer on June 6, 2026 at 7:48 pm

    Personally I think you should do three things:

    1. Absolutely have the unique constraint to protect the data at its most basic level.
    2. Check for potential violations before blindly inserting rows.
    3. Wrap TRY/CATCH around the eventual insert to protect users from exceptions where the check fails.

    For single-row inserts, this can be as simple as:

    BEGIN TRY
      INSERT dbo.TEST(ValA, ValB, ValC, ValD)
        SELECT @ValA, @ValB, @ValC, @ValD
        WHERE NOT EXISTS
        (SELECT 1 FROM dbo.TEST
          WHERE ValA = @ValA AND ValB = @ValB AND ValC = @ValC AND ValD = @ValD);
    END TRY
    BEGIN CATCH
      PRINT 'Move along, nothing to see here...';
    END CATCH
    

    It gets a little more complicated if any or all of these columns are nullable.

    For multi-row inserts, you can handle this in a variety of ways. You can have the entire batch fail if there is a non-unique value (either in the batch alone or conflicting with the table), or you can allow just the successful rows into the table. Situation A:

      IF EXISTS (SELECT 1 FROM @SourceOfMultipleRows AS r
        WHERE EXISTS (SELECT 1 FROM dbo.Test AS t WHERE t.ValA = r.ValA AND ...))
      OR EXISTS (SELECT 1 FROM @SourceOfMultipleRows
        GROUP BY ValA, ValB, ValC, ValD HAVING COUNT(*) > 1)
      BEGIN
        PRINT 'Not proceeding at all.';
      END
      ELSE
      BEGIN
        BEGIN TRY
          INSERT dbo.TEST(ValA, ValB, ValC, ValD)
            SELECT ValA, ValB, ValC, ValD
            FROM @SourceOfMultipleRows AS r
            WHERE NOT EXISTS (SELECT 1 FROM dbo.Test AS t
              WHERE t.ValA = r.ValA AND ...)
            GROUP BY ValA, ValB, ValC, ValD;
        END TRY
        BEGIN CATCH
          PRINT 'Move along, nothing to see here...';
        END CATCH
      END
    

    Scenario B, where you want to keep the good rows and ignore duplicates:

    BEGIN TRY
      INSERT dbo.TEST(ValA, ValB, ValC, ValD)
        SELECT ValA, ValB, ValC, ValD
        FROM @SourceOfMultipleRows AS r
        WHERE NOT EXISTS (SELECT 1 FROM dbo.Test AS t
          WHERE t.ValA = r.ValA AND ...)
        GROUP BY ValA, ValB, ValC, ValD;
    END TRY
    BEGIN CATCH
      PRINT 'Move along...';
    END CATCH
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read this article here that talks about progressive enhancement for javascript and the
There is a post on this topic already, but it does not have an
There are several talks about this issue, but I havent found a clear complete
I know there is this post , but I still want to know more
Referring back to this SO post If there is a Grouping category Category which,
This question is related with one of my earlier questions.. Previous Post In there
So I saw this post here and read it and it seems like bulk
I have a basic PHP form here that sends an email when submitted: <div
Similar to another post I made, this answers that post and creates a new
I have a site that gets a user data on logging in. this data

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.