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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:07:11+00:00 2026-06-02T16:07:11+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.

Conditions are if:
1) the values of id and ahccs are same in both the table, then don’t do anything.
2) the value of id are same but ahccs are different, then set flag =’z’ and insert the same id with the new ahccs value.

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

I might 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 
WHEN MATCHED AND t2.ahccs != t1.ahccs THEN
UPDATE
SET     flag = 'z'
INSERT VALUES (t2.id, 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-02T16:07:13+00:00Added an answer on June 2, 2026 at 4:07 pm

    I think I understand the requirements now. You want to update records in table2 where the id matches but ahccs does not, setting the flag to ‘z’. Additionally, for these mismatches, a new row should be inserted into table2 with the same id but the ahccs from table1, and a flag of ‘l’.

    DECLARE @tmp TABLE (id int, ahccs int, info varchar(25), flag varchar(2))
    MERGE
    INTO    @table2 t2
    USING   @table1 t1
    ON      t2.id = t1.id
    WHEN NOT MATCHED THEN
        INSERT VALUES (t1.id, t1.ahccs, t1.info, t1.flag)
    WHEN MATCHED and t2.ahccs <> t1.ahccs THEN
        UPDATE SET flag = 'z'
    output inserted.id, t1.ahccs, t1.info, inserted.flag
    into @tmp;
    
    insert into @table2
    select id, ahccs, info, 'l' as flag
    from @tmp
    where flag = 'z' -- don't insert what we've already inserted
    
    • 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.