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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:22:45+00:00 2026-06-15T21:22:45+00:00

INSERT INTO TABLE1 (COLUMN1, PRIMARY_KEY) SELECT COLUMN1, (SELECT COALESCE(MAX(PRIMARY_KEY), 0) FROM TABLE1) + 1

  • 0
INSERT INTO TABLE1 (COLUMN1, PRIMARY_KEY)
   SELECT 
      COLUMN1,
      (SELECT COALESCE(MAX(PRIMARY_KEY), 0) 
        FROM TABLE1) + 1 
   FROM 
      TABLE2

error:

Violation of Primary Key constraint. Cannot insert duplicate key in object.

How do I make the primary key increment after the first row?

I would like to be able to add a list of items to this table at the same time instead of inserting them RBAR.

Thanks for the help

  • 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-15T21:22:46+00:00Added an answer on June 15, 2026 at 9:22 pm
    INSERT INTO TABLE1 (COLUMN1, PRIMARY_KEY)
    SELECT COLUMN1,
           (SELECT COALESCE(MAX(PRIMARY_KEY),0)
           FROM TABLE1) + row_number() over (order by 1/0)
    FROM TABLE 2
    

    For this statement alone, the IDs will be sequential, e.g. if Max(Primary Key) is 99 and it is inserting 4 records, they will be 100, 101, 102, 103. It’s very prone to constraint violations if multiple processes are inserting at the same time, but that’s not to say it is any worse than what you have with a single record anyway using MAX() which is inherently unsafe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to insert data from table1 into table2. However, I would like to
INSERT INTO table_a (col_tinyint, col_varchar) SELECT 0, distinct(col_other_varchar) COLLATE utf8_bin FROM table_b I want
I'm running a pretty standard INSERT INTO [table] (col1, col2, ...coln) select * from
Assuming CREATE TABLE TableA ( IntColumn INT NOT NULL PRIMARY KEY ) INSERT INTO
The following code will return select $f/field/text() from table1 declare @x xml = '<meta><field>Column1</field></meta>';
If I insert the following values into my table Insert into Table1 (Field1, Field2,
what i am trying to do is this: INSERT INTO Table1 (id1, id2) (
I get an error when trying to insert into TABLE_EVENTS but no problem inserting/querying
This is my Insert Statement INSERT INTO ProductStore (ProductID, StoreID, CreatedOn) (SELECT DISTINCT(ProductId), 1,
I am trying to populate the records from table1 to table2. table1 has three

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.