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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:37:16+00:00 2026-05-20T17:37:16+00:00

I have 2 tables with the same columns – T1, T2. I want to

  • 0

I have 2 tables with the same columns – T1, T2. I want to update table T1 columns from the columns that exist in T2 based on the key column: if the key column exists then update the rest of the columns of T1 from T2, if doesnt exist, then insert the whole row from T2 to T1.

This query doesnt do the job:

IF EXISTS (SELECT keyC FROM T2 WHERE keyC in (select keyC from T1)) 
UPDATE T1 SET T1.c1 = T2.c1,
T1.c2 = T2.c2,
from  T2 WHERE  T2.keyC in (select keyC from T1)
ELSE (INSERT INTO T1 select * from T2)

Any idea how to fix it?

Thanks in advance,

Greg

  • 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-05-20T17:37:17+00:00Added an answer on May 20, 2026 at 5:37 pm

    Since you use SQL Server 2005 you can’t use merge and have to do two statements. One update and one insert.

    declare @T1 table (keyC int, c1 int)
    declare @T2 table (keyC int, c1 int)
    
    insert into @T1 values (1, 1)
    
    insert into @T2 values (1, 10)
    insert into @T2 values (2, 20)
    
    -- Update all rows
    update @T1 set
      c1 = T2.c1
    from @T1 as T1
      inner join @T2 as T2
        on T1.keyC = T2.keyC
    
    -- Insert new rows
    insert into @T1 (keyC, c1)
    select keyC, c1
    from @T2 as T2
    where not exists (select *
                      from @T1 as T1
                      where T1.keyC = T2.keyC)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables with same columns. i want to update table1 records whose
I have two tables, both with the same columns. The first table contains original
I have four tables containing exactly the same columns, and want to create a
I have two tables that contain completely separate information: table: tires columns: Tire_id name
supposing that I have two tables with same 3 columns: ID (int, identity), text
I have multiple html tables. Each table has the same number of columns but
I have 3 tables that have same columns,but different data ( deposits,withdrawals,transfers ) CREATE
There are two tables: table A and table B. They have the same columns
I have two tables: <class name=Content table=language_content lazy=false> <composite-id> <key-property name=contentID column=contentID/> <key-property name=languageID
I have 3 database tables, all of them have the same 5 columns. They

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.