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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:25:33+00:00 2026-06-11T12:25:33+00:00

I have stored a result set on a temporary table which has Column_Name, Data_Type,

  • 0

I have stored a result set on a temporary table which has

Column_Name, Data_Type, Default_Value
Col1         varchar      abc
Col2         varchar      abc
Col3         int          999
Col4         decimal      0.0
Col5         datetime     20120101

Now I have to build a statement which will Compare the column name with the column name of the table and based on the datatype of the temporary table it will assign the default value to it whenever column value is NULL.

I have no clue how to do that

This is what I have done till now

  CREATE TABLE #ColumnData (Column_Name varchar(500), Data_Type varchar(20),

Default_Value varchar(50))

 DECLARE @query1 NVARCHAR(MAX) = '' 
  DECLARE @SchemaName VARCHAR(20)
  DECLARE @TableName VARCHAR(50)

SET @SchemaName = 'dbo'
SET @TableName = 'tblCustomer'





     select @query1 = @query1 + 'UPDATE ' + @schemaName + '.' + @tableName + ' SET ' + 
     c.COLUMN_NAME + ' = ' + c.Default_Value+ ' WHERE ' +   c.COLUMN_NAME 
     + ' = ''NULL'';' FROM #ColumnData c  
     WHERE C.TABLE_NAME = @tableName     

      EXECUTE sp_executesql @query1 
  • 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-11T12:25:35+00:00Added an answer on June 11, 2026 at 12:25 pm

    This should do the trick. Tested it out in SQL 2008.

    update t
    set t.col1 = isnull(t.col1, tr.col1) 
        t.col2 = isnull(t.col1, tr.col2)
        etc.
    from my_table t
    cross join temp_table tr
    

    Update:

    — create your temp table like this, then set all the default values in a single row (note the _temp on the end of the temp table name).

    select * into #MyTable_temp from dbo.MyTable where 1=1
    

    DECLARE @query NVARCHAR(MAX) = 'update t set ' 
    DECLARE @SchemaName VARCHAR(20) 
    DECLARE @TableName VARCHAR(50) 
    
    SET @SchemaName = 'dbo' 
    SET @TableName = 'MyTable' 
    
    set @query = @query + ( 
            select stuff((
                select ', ' + c.column_name + ' = isnull(t.' + c.column_name + ', tt.' + c.column_name + ')'
                from INFORMATION_SCHEMA.COLUMNS c
                where c.TABLE_NAME = @TableName
                for xml path('')), 1, 2, ''))
    
    set @query = @query + ' from ' + @SchemaName + '.' + @TableName + ' t cross join #' + @TableName + '_temp tt'
    
    select @query
    

    Your query should look like this:

    update t 
        set Col1 = isnull(t.Col1, tt.Col1), 
            Col2 = isnull(t.Col2, tt.Col2), 
            Col3 = isnull(t.Col3, tt.Col3), 
            Col4 = isnull(t.Col4, tt.Col4), 
            etc... 
    from dbo.MyTable t 
    cross join #MyTable_temp tt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure/ routine ABC which returns me a table with few
I have a MYSQL stored procedure SP1() that returns a result set. I want
I have a table from which I query data to display resultset using stored
I have a table 'results' in which the scores are stored. Its ordered by
I have a stored procedure that returns a result set (4 columns x n
I have a stored procedure in Oracle that returns result set(s) via OUT parameter(s)
I need to store the result set of a stored procedure in a temporary
I am creating a stored procedure which has to create a table which name
I have a stored procedure that needs to pass a result set from another
I have a stored procedure which returns multiple result sets similiar to the following:

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.