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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:52:29+00:00 2026-06-02T17:52:29+00:00

I have a situation where I need to insert data from table1 to table2.

  • 0

I have a situation where I need to insert data from table1 to table2. Before insert check if a certain row already exist in the table2, if it does then just update col2, col4 of the row. If it doesn’t exist then insert a new row.

I am using SQLSERVER 2008 R2. How could I achieve this?

The situation has changed a bit now. I need something like this.

DECLARE @table1 TABLE 
(id int not null, ahccs int not null, info varchar(25), flag varchar(2))
DECLARE @table2 TABLE 
(id int not null, ahccs int not null, info varchar(25), flag varchar(2))

INSERT INTO @table1
VALUES(1, 1223, 'et', 'X')
INSERT INTO @table1
VALUES(2, 321, 'et', 'X')
INSERT INTO @table1
VALUES(3, 134, 'et', 'X' )
INSERT INTO @table1
VALUES(4, 168, 'et', 'X' )
INSERT INTO @table1
VALUES(5, 123, 'et', 'X' )


INSERT INTO @table2
VALUES(1, 1223, 'dt', 'y' )
INSERT INTO @table2
VALUES(2, 456, 'dt', 'y' )
INSERT INTO @table2
VALUES(3, 123, 'dt', 'y' )
INSERT INTO @table2
VALUES(4, 193, 'dt', 'y' )
--SELECT * FROM @table1

SELECT * FROM @table2

MERGE
INTO    @table2 t2
USING   @table1 t1
ON      t2.id = t1.id or t2.ahccs = t1.ahccs
WHEN NOT MATCHED THEN
UPDATE
SET     flag = 'z'
INSERT VALUES (100, t1.ahccs, t1.info, 'l');

The two issues I am having are:
1) Merge doesn’t support multiple steps, I believe.
2) Update is not allowed in WHEN NOT MATCHED case.

Please advise.

Thank You.

  • 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-02T17:52:30+00:00Added an answer on June 2, 2026 at 5:52 pm
    MERGE
    INTO    table2 t2
    USING   table1 t1
    ON      t2.id = t1.t2_id
    WHEN NOT MATCHED THEN
    INSERT  
    VALUES  (t1.col1, t1.col2, ...)
    WHEN MATCHED THEN
    UPDATE
    SET     col2 = t1.col2,
            col4 = t1.col4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation where I need to insert data from table1 to table2.
We have a situation where we need to display data from a database in
I have a situation similar to the following question: Insert Data Into SQL Table
I have this situation where I need to modify the insert command after is
I am using EF4 and I have a situation where I need to insert
I have a situation where I pull data from a table by date. If
I have situation where I need to change the order of the columns/adding new
I have a situation where I need to update a control referenced in a
I have a situation where I need to notify some users when something in
I have a situation where I need to find the value with the key

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.