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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:53:08+00:00 2026-06-10T20:53:08+00:00

This is probably a familiar problem but i didn’t find an answer in searches.

  • 0

This is probably a familiar problem but i didn’t find an answer in searches. I do have a solution (shown below) that involves COALESCE on every non-key column, I’m wondering if there’s a Dynamic TSQL routine, or perhaps a different way of forumulating the query that doesn’t involve explicitly naming all the columns.

I have two tables, “targetTable” and “deltaTable”, that have a the same field names and types. Both have a unique key, “id”.

Deltatable has data that has changed for a corresponding row in targetTable. Any column where the value hasn’t changed is null. I want to update the columns in the targetTable that have non-null values in the corresponding row of the deltaTable. DetlaTable.id is guaranteed to exist in TargetTable.id, and I never want to update a field to NULL.

I’m using SQL Server 2005 (which doesn’t have the merge statement, not that I know it would help).

So if a targetTable row looks like

  id | name    |  dob        | next_appt
  5    Bill       1-1-2012   | 3-3-2015

and a deltaTable row looks like

  id | name     | dob        | next_appt
  5  | NULL     | NULL       | 4-4-2015

I want targetTable updated to

  id | name    |  dob        | next_appt
  5    Bill       1-1-2012   | 4-4-2015

What I have now that I want to improve on is:

UPDATE target
SET target.colA = COALESCE(delta.colA, target.colA),
    target.colN = COALESCE(delta.colN, target.colN)
FROM  delta JOIN target on delta.id = target.id
  • 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-10T20:53:09+00:00Added an answer on June 10, 2026 at 8:53 pm

    I’m not sure it’s a great idea, but if you really want to do it, you could do something like this:

    DECLARE @sql nvarchar(max)
    SET @sql = N'UPDATE target SET
    ';
    SELECT @sql = @sql + N'  target.'+QUOTENAME([name]) + ' = COALESCE(delta.' + QUOTENAME([name]) + ', target.' + QUOTENAME([name]) + '),
    '
    FROM sys.columns 
    where object_id = object_id('target') AND [name] != 'id'
    order by [column_id]
    
    SET @sql = STUFF(@sql,LEN(@sql)-2,1,''); -- strip off the last commma
    set @sql = @sql + 
    'FROM delta 
    JOIN target ON  delta.id = target.id'
    
    EXECUTE sp_executesql @sql
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This probably is a dummy question but I cannot find a clear indication. I
This problem is probably very simple to solve but it is not clear to
Really basic (and probably quite idiotic) question but I'm not familiar with this procedure
Probably any experienced web developer would be familiar with this problem: over time your
This probably has a one click solution, i know debugging web pages in VS2010
This probably sounds like a terrible idea at first glance, but here is my
I know this probably really simple but Im not sure what im doing wrong...
I know this probably has been asked before but I am having issues with
Im sorry for this probably dumm question, but I want to simply open modals
I am using ASP.Net MVC but this probably applies to all MVC patterns 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.