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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:26:27+00:00 2026-05-11T10:26:27+00:00

This may have been asked before, but it’s really hard to search for terms

  • 0

This may have been asked before, but it’s really hard to search for terms that limit the search results…

Take the following SQL snippet:

declare @source table (id int) declare @target table(id int primary key, sourceId int)   set nocount on  insert into @target values (0,0)  insert into @source(id) values(1) --insert into @source(id) values(2)  set nocount off  insert into @target select (select max(id)+1 from @target), s.id from @source s  select * from @target 

This obviously executes without error, but now uncomment the second insert line and the following error occurs:

Msg 2627, Level 14, State 1, Line 15 Violation of PRIMARY KEY constraint 'PK__#7DB3CB72__7EA7EFAB'. Cannot insert duplicate key in object 'dbo.@target'. 

I realise that the insert statement more than likely is effected against a snapshot of the @target table so (select max(id)+1 from @target) will always return a value of 1 – causing the violation error above…

Is there any way around this apart from resorting to a cursor?

  • 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. 2026-05-11T10:26:28+00:00Added an answer on May 11, 2026 at 10:26 am

    Change your insert statement to the following:

    insert into @target select (select max(id) from @target) + (ROW_NUMBER() OVER(ORDER BY s.id)), s.id from @source s

    This should work for this specific case but I would be careful about generalizing it.

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

Sidebar

Related Questions

I'm aware that this question may have been asked before, but I still haven't
This question may have been asked before, but I had trouble finding an answer,
This has / may have been asked before, but, as far as I remember,
This may have been asked before, but I could not find the answer. I
This question may have been asked before, but I'm starting to get into game
this may have been asked already but I cant really find it (most likely
First, this question may have been asked before, but I'm not sure what phrase
This question may well have been asked before but I didn't find anything whilst
This question has been asked before, but I can't find any answers that have
this may have been asked before, but I could not find it. Suppose I

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.