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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:17:54+00:00 2026-05-26T09:17:54+00:00

I have two SQL Server tables. One table has four nvarchar(50) nullable columns and

  • 0

I have two SQL Server tables. One table has four nvarchar(50) nullable columns and an ID column. The second table has two columns: ID, nvarchar(50) nullable column

I’m trying to insert the rows in the second table into the first table. How can I detect a null column in the first table and insert data from the second one into the null cell?

Ex:

Add from the second table

1   value3

to the first table

1     value1   value2   null    null

so the resulting row is

1     value1   value2   value3   null

Ex2:

Add from the second table

2   value1

to the first table

nothing for ID 2

so the resulting row is

2     value1   null   null   null

Basically I need to update existing rows or create new ones depending on the ID of the second table rows.

  • 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-26T09:17:55+00:00Added an answer on May 26, 2026 at 9:17 am
    declare @T1 table
    (
      ID int primary key,
      C1 varchar(50),
      C2 varchar(50),
      C3 varchar(50),
      C4 varchar(50)
    );
    
    declare @T2 table
    (
      ID int primary key,
      C varchar(50)  
    );
    
    insert into @T1 values (1, 'value1', 'value2', null, null) ;
    
    insert into @T2 values (1, 'value3');
    insert into @T2 values (2, 'value1');
    
    merge @T1 as T1
    using @T2 as T2
    on T1.ID = T2.ID
    when not matched then
      insert (ID, C1)  values (T2.ID, T2.C)
    when matched then
      update set
        C1 = case when T1.C1 is null 
               then T2.C 
               else T1.C1 
             end,
        C2 = case when T1.C1 is not null and 
                       T1.C2 is null 
               then T2.C 
               else T1.C2 
             end,
        C3 = case when T1.C1 is not null and 
                       T1.C2 is not null and 
                       T1.C3 is null 
               then T2.C 
               else T1.C3 
             end,
        C4 = case when T1.C1 is not null and 
                       T1.C2 is not null and 
                       T1.C3 is not null and 
                       T1.C4 is null 
               then T2.C 
               else T1.C4 
             end
    ;
    
    select *
    from @T1;
    

    Result:

    ID C1     C2     C3     C4
    -- ------ ------ ------ ------
    1  value1 value2 value3 NULL
    2  value1 NULL   NULL   NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SQL Server 2008: I have two tables, dtlScheme and dtlRenewal, with a one
I have two related sql server tables ... TableA and TableB. ***TableA - Columns***
I have a SQL Server 2008 database that has two tables. These two tables
I have two SQL Server tables with Primary Keys (PK) and a Foreign Key
We have two tables in a SQL Server 2005 database, A and B.There is
Good Afternoon All, I have two tables in my SQL Server 2005 DB, Main
I have SQL Server 2008 Ent and OLTP database with two big tables. How
I have two SQL Server 2005 databases, one is for development and the other
I have two SQL Server 2008 databases called Anna and Bob . Bob has
I have two named instances of SQL Server 2008 and am trying to set

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.