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

  • Home
  • SEARCH
  • 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 8183969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:20:21+00:00 2026-06-07T01:20:21+00:00

In SQL server, I’m using a table variable and when done manipulating it I

  • 0

In SQL server, I’m using a table variable and when done manipulating it I want to insert its values into a real table that has an identity column which is also the PK.

The table variable I’m making has two columns; the physical table has four, the first of which is the identity column, an integer IK. The data types for the columns I want to insert are the same as the target columns’ data types.

 INSERT INTO [dbo].[Message] ([Name], [Type])
 SELECT DISTINCT [Code],[MessageType] 
 FROM @TempTableVariable    

This fails with:

Cannot insert duplicate key row in object ‘dbo.Message’ with unique index
‘IX_Message_Id’. The duplicate key value is (ApplicationSelection).

But when trying to insert just Values (…) it works ok.

How do I get it right?

  • 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-07T01:20:22+00:00Added an answer on June 7, 2026 at 1:20 am

    It appears that the data “ApplicationSelection” is already in the database. YOu need to write the select to exclude records that are already in the database. YOu can do that with a where not exists clause or a left join. LOok up teh index to see what field is unique besides the identity. That will tell you what feild you need to check to see if teh record currently exists.

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

Sidebar

Related Questions

SQL SERVER, I'm working with a table that uses a guid for the key
Using SQL Server 2005 Leave Table ID StartDate EndDate 001 02/03/2010 02/03/2010 002 02/03/2010
Sql Server is accessible. I want to migrate database using Entity Framework migrations. There
SQL Server 2000 I backup a table like below: select * into bkp_table from
SQL Server 2005 I have an SQL Function (ftn_GetExampleTable) which returns a table with
SQL Server 2008 Profiler always profiles all databases. I want to restrict profiling or
SQL Server a stored procedure written in C# on the .NET 2.0 framework that
SQL Server 2005 Profiler shows that a Stored Procedure (SP) was called and what
SQL Server 2008 R2: normally, we create our table, and stored procedure, and grant
SQL Server 2005/8 allows you to associate a synonym with a remote table (i.e.

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.