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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:16:32+00:00 2026-06-02T03:16:32+00:00

I am trying to resolve t-sql exercise I need to update first table with

  • 0

I am trying to resolve t-sql exercise
I need to update first table with values from second by joining by id. If I can not join then use value from default ID (default iD is the Id that is null)

please run it to see it

declare @t as table (
    [id] INT
    ,val int
)

insert into @t values (null, null)
insert into @t values (2, null)
insert into @t values (3, null)
insert into @t values (4, null)

declare @t2 as table (
    [id] INT
    ,val int
)

insert into @t2 values (null, 11)
insert into @t2 values (2, 22)
insert into @t2 values (3, 33)


select * from @t
select * from @t2

update t
set t.val = t2.val
from @t as t join @t2 as t2
    on t.id = t2.id
        or 
        (
            (t.id is null or t.id not in (select id from @t2))
            and t2.id is null
        )




select * from @t

here is result

--@t
id      val
---------------
NULL    NULL
2       NULL
3       NULL
4       NULL

--@t2
id      val
---------------
NULL    11
2       22
3       33

--@t after update
id      val
---------------
NULL    11
2       22
3       33
4       NULL

how to make val in last row equal 11?

4       11
  • 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-02T03:16:34+00:00Added an answer on June 2, 2026 at 3:16 am

    try this for the update…

    update t
    set t.val = t2.val
    from @t as t join @t2 as t2
        on t.id = t2.id
            or 
            (
                (t.id is null or not exists (select * from @t2 where id = t.id))
                and t2.id is null
            )
    

    Problem is with not in operator and null values. This would also work…

    update t
    set t.val = t2.val
    from @t as t join @t2 as t2
        on t.id = t2.id
            or 
            (
                (t.id is null or t.id not in (select id from @t2 where id is not null))
                and t2.id is null
            )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get distinct values from table application based on the column
I am trying to resolve a 100% cpu usage by the SQL Server process
I'm trying to resolve a hostname in PHP and can't use the builtin gethostbyname
I am going nuts here trying to resolve a cascading update/delete issue :-) I
I trying to and a non-unique index to a table table in SQL Server
really struggling to resolve this issue. using nhibernate Im trying to join two different
Hello I'm trying to write some values to my SQL through the C# WinForm
I have a SQL Server error I'm trying to resolve. Could someone please help
I am running into problem when working with linq-to-sql and trying to resolve conflicts.
I am trying to select from the join of the two related tables in

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.