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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:51:52+00:00 2026-06-08T19:51:52+00:00

I am trying to update a table so that rows that are missing are

  • 0

I am trying to update a table so that rows that are missing are added and rows that are not up to date are updated using information from another table on a different database as a reference.

However, some tables have primary keys and some do not.
If there is a primary key the insert command will not run and if there is not a primary key, rows will duplicate.

Is there a way to have the insert command skip over primary key values that are already there?

I’m using sql server management studio 2005 and here is the code I have so far for a table with a primary key (PKcolumn):

INSERT [testDB].[dbo].[table1]
SELECT * FROM [sourceDB].[dbo].[table1]

UPDATE test
SET 
 test.[PKcolumn] = source.[PKcolumn]
,test.[column2] = source.[column2]
,test.[column3] = source.[column3] 
FROM 
[sourceDB].[dbo].[sourceDB] AS source
INNER JOIN 
[testDB].[dbo].[PKcolumn] AS test
ON source.[PKcolumn] = test.[PKcolumn]

Update works perfectly but Insert will not run at all if there is even one duplicate.

Any suggestions on how to make this code work?

Also, any tips for doing the same thing on a table without a primary key?

  • 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-08T19:51:54+00:00Added an answer on June 8, 2026 at 7:51 pm

    You’ll need to exclude rows that are already present in the table in your INSERT query, using a LEFT OUTER JOIN:

    INSERT [testDB].[dbo].[table1]
    SELECT * FROM [sourceDB].[dbo].[table1]
    LEFT OUTER JOIN [testDB].[dbo].[table1] ON [sourceDB].[dbo].[table1].[PKcolumn] = [testDB].[dbo].[table1].[PKcolumn]
    WHERE [testDB].[dbo].[table1].[PKcolumn] IS NULL
    

    For a table without a primary key, I suppose you’d need to join on ALL columns to avoid duplicates.

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

Sidebar

Related Questions

I am trying to update rows in a data table that intersect rows in
I'm trying to update a table based upon the user id from another table.
I have been trying to update a date for all rows in a table
I have a heap based table in MySQL that I am trying to update
I am trying to update table A with data from table B. I thought
I'm trying to update a table in my DynamoDB tables using the PHP SDK.
I'm trying to update a table from a form. I have 3 pages. The
I'm trying to update one table based on values in another table. What's wrong
I have a MySQL table that's being updated very frequently. In essence, I'm trying
I'm trying to append rows upon scrolling of a table using a modified version

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.