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

The Archive Base Latest Questions

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

I need a SQL statement which fills the null values from the second column

  • 0

I need a SQL statement which fills the null values from the second column of #T1 table with values from #T2(C1).

There is no foreign key or match between the columns of those two tables.

Sample:

T1 (C1, T2C1)
A1, 1
A2, null
A3, null
A4, 4
A5, null
-------------
T2 (C1)
a
b

After update, the T1 will look like:

A1, 1
A2, a
A3, b
A4, 4
A5, null

I found two approaches:

Using CTE

create table #T1 (C1 varchar(10), T2C1 varchar(10))
create table #T2 (C1 varchar(10))

insert into #T1 values ('A1', '1')
insert into #T1 values ('A2', null)
insert into #T1 values ('A3', null)
insert into #T1 values ('A4', '4')
insert into #T1 values ('A5', null)

insert into #T2 values ('a')
insert into #T2 values ('b')

;with t2 as
(
select C1, row_number() over (order by C1) as Index2
from #T2
)
,t1 as
(
select T2C1, row_number() over (order by C1) as Index1   
from #T1
where T2C1 is null
)
update t1
set t1.T2C1 = t2.C1
from t2
where t1.Index1 = t2.Index2

select * from #T1

drop table #T1
drop table #T2

With Derived Tables

create table #T1 (C1 varchar(10), T2C1 varchar(10))
create table #T2 (C1 varchar(10))

insert into #T1 values ('A1', '1')
insert into #T1 values ('A2', null)
insert into #T1 values ('A3', null)
insert into #T1 values ('A4', '4')
insert into #T1 values ('A5', null)

insert into #T2 values ('a')
insert into #T2 values ('b')

update #T1
set T2C1 = cj.C1
from #T1
join (select T2C1, row_number() over (order by C1) as Index1, C1
   from #T1
   where T2C1 is null) ci on ci.C1 = #T1.C1
join (select C1, row_number() over (order by C1) as Index2
  from #T2) cj on ci.Index1 = cj.Index2

select * from #T1

drop table #T1
drop table #T2

My question is, can I achieve this without using windowing functions and with no cursors?

Update

@Damien_The_Unbeliever correctly points that to do this kind of update it is not possible without defining an ordering on tables, actually I think exactly said is without properly identify and link the rows from target table.

@Bogdan Sahlean has found another way, using table variables and IDENTITY column, which I’m happy with this solution, it’s another way
However, in the real application I will still use the windowing functions

Thanks all

  • 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-26T06:20:16+00:00Added an answer on May 26, 2026 at 6:20 am

    1.I suppose you have a pk in target table (#T1).

    2.Instead of ROW_NUMBER this solution uses IDENTITY(1,1) columns and two table variables.

    3.I didn’t tested this solution.

    DECLARE @t2_count INT = (SELECT COUNT(*) FROM #T2);
    
    DECLARE @Target TABLE
    (
         MyId INT IDENTITY(1,1) PRIMARY KEY
        ,T1_pk INT NOT NULL UNIQUE
    );
    INSERT  @Target (T1_pk)
    SELECT  TOP(@t2_count) pk
    FROM    #T1 
    WHERE   T2C1 IS NULL;
    
    DECLARE @Source TABLE
    (
         MyId INT IDENTITY(1,1) PRIMARY KEY
        ,C1 VARCHAR(10) NOT NULL
    );
    INSERT  @Source (C1)
    SELECT  C1
    FROM    #T2;
    
    UPDATE  #T1
    SET     T2C1 = src.C1
    FROM    #T1 t
    INNER JOIN @Target trg ON t.pk = trg.T1_pk 
    INNER JOIN @Source src ON trg.MyId = src.MyId;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Statement where i need to display the value from another
Nightly, I need to fill a SQL Server 2005 table from an ODBC source
I have the following Execution statement which creates a table (using data from another
Which SQL statement is faster? SELECT TOP 2 c1.Price, c2.Price, ..... c49.Price, c50.Price FROM
Basically I need to return some data from a SQL Server table in the
This SQL statement example is very close to what I think I need... update
I need to transform an Oracle SQL statement into a Stored Procedure therefore users
I have the following SQL Statement. I need to select the latest record for
I have a dynamic SQL statement I've created in a stored procedure. I need
I an SQL SELECT statement I need to extract the name of two teams,

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.