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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:19:48+00:00 2026-05-15T21:19:48+00:00

Is there a way to select the row from a temp table (table has

  • 0

Is there a way to select the row from a temp table (table has only one row anyway), into another table that has some columns with differenet names? For example:

TempTable

FirstName        LastName          Column1       Column2
------------    ------------     -----------   -----------
Joe              Smith             OKC           TX

OtherTable

FirstName       LastName          Region1        Region2        Region3
------------    ------------     -----------   -----------     ----------
NULL              NULL             NULL           NULL           NULL

I need to copy the data, in the same order as the columns from TempTable into OtherTable. TempTable will not always be the same….as in sometimes it will have 3 columns, sometimes just 2…etc. If it does not have the same number of columns as OtherTable, the the remaining “Region” columns should stay null.

The end result should be:

OtherTable

FirstName       LastName         Region1       Region2         Region3
------------    ------------     -----------   -----------     ----------
Joe              Smith             OKC           TX               NULL

PLUS the column names in TEMPTable will NEVER be the same…as in one time it will be “Column1″…the next time it could be “XXXXX1”. That’s why I just want to copy data only…the data will always be in the correct order…

LOL…does this even make sense? This is for SQL Server 2005

  • 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-15T21:19:48+00:00Added an answer on May 15, 2026 at 9:19 pm

    EDIT …….. Dynamic SQL Generation added

    This code will generate INSERT statements to INSERT from #TEMP into #TEMP. You can tweak it to suit your purpose if you are going from #temp to regular tables.

    SET NOCOUNT ON
    DROP Table #TempTable1
    DROP Table #TempTable2
    GO
    DROP Function GenerateInserts
    GO
    Create Function GenerateInserts
    (
        @SourceTable    VarChar (100),
        @DestinationTable   VarChar (100)
    )
    Returns VarChar (MAX)
    AS
    BEGIN
    
    DECLARE @SelectString VarChar (MAX)
    DECLARE @InsertString VarChar (MAX)
    DECLARE @SQLString VarChar (MAX)
    
    DECLARE @SourceColumnCount  INTEGER
    DECLARE @DestinationColumnCount INTEGER
    DECLARE @ColumnCount    INTEGER
    DECLARE @Counter    INTEGER
    
    SELECT @SourceColumnCount = COUNT (*)
    FROM tempdb..syscolumns 
    WHERE id=object_id(@SourceTable)
    
    SELECT @DestinationColumnCount = COUNT (*)
    FROM tempdb..syscolumns 
    WHERE id=object_id(@DestinationTable)
    
    SET @ColumnCount = @SourceColumnCount
    
    IF @DestinationColumnCount < @ColumnCount
        SET @ColumnCount = @DestinationColumnCount
    
    SET @Counter = 0
    
    SET @SelectString = ' INSERT INTO ' + @DestinationTable + ' '
    SET @InsertString = ' INSERT INTO ' + @DestinationTable + ' '
    
    SET @SelectString = ''
    SET @InsertString = ''
    
    WHILE @Counter <= @ColumnCount
    BEGIN
        SELECT @SelectString = @SelectString  + ', ' + Name
        FROM TempDB..SysColumns 
        WHERE Id = Object_Id (@SourceTable)
        AND ColOrder = @Counter
    
        SELECT @InsertString = @InsertString  + ', ' + Name
        FROM TempDB..SysColumns 
        WHERE Id = Object_Id (@DestinationTable)
        AND ColOrder = @Counter
    
        SET @Counter = @Counter  + 1
    END
    
    SET @InsertString = 'INSERT INTO ' + @DestinationTable + ' (' +  STUFF (    @InsertString, 1, 2, '') + ') '
    SET @SelectString = 'SELECT ' +  STUFF (    @SelectString, 1, 2, '') + ' FROM ' + @SourceTable
    
    SET @SQLString = @InsertString + '
    '+ @SelectString
    
    RETURN @SQLString
    END
    
    GO
    
    Create Table #TempTable1
    (
        Col1 VarChar (10), 
        Col2 VarChar (10), 
        Col3 VarChar (10),
        Col4 VarChar (10), 
        Col5 VarChar (10) 
    )
    Create Table #TempTable2
    (
        MyCol1 VarChar (10), 
        MyCol2 VarChar (10), 
        MyCol3 VarChar (10),
        MyCol4 VarChar (10), 
        MyCol5 VarChar (10), 
        MyCol6 VarChar (10)
    )
    
    SELECT dbo.GenerateInserts ('tempdb..#TempTable1', 'tempdb..#TempTable2')
    



    OLD ANSWER

    Yes you can do this but you have to write different statements for each type of INSERT. You do have to specify column names in both places – the INSERT INTO and the SELECT


    If you have the same number of columns in your Source and Destination tables, do this

    INSERT INTO Table1 (Column1, Column2, Column3)
    SELECT MyColumn01, MyColumn02, MyColumn03
    FROM MyTable 
    

    What this will do is map as follows:

    MyTable.MyColumn01 -> Table1.Column1
    MyTable.MyColumn02 -> Table1.Column2
    MyTable.MyColumn03 -> Table1.Column3


    If the Source has less columns, you can use a NULL value in place of the column name

    INSERT INTO Table1 (Column1, Column2, Column3)
    SELECT MyColumn01, MyColumn02, NULL AS MyColumn03
    FROM MyTable 
    

    OR you can just use two column names

    INSERT INTO Table1 (Column1, Column2)
    SELECT MyColumn01, MyColumn02
    FROM MyTable 
    

    If the destination table has less columns than the source, then you have to ignore columns from the source

    INSERT INTO Table1 (Column1, Column2, Column3)
    SELECT MyColumn01, MyColumn02, NULL AS MyColumn03 /* MyColumn04, MyColumn05 are ignored */
    FROM MyTable 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to select only innermost tables? That is ones that do
I'm trying to figure out how i could select a row from there table
I am trying to get data from one row in a table into three
I am attempting to insert a copy of a row from one table into
Is there any way to select items in a list that aren't contained in
1) Is there any way to select a random record from Freebase? If I
I have some sprocs that need a temp table. In order not to hardcode
Is there any way I can get the actual row number from a query?
Is there a way to coerce the result row gotten from calling a stored
Is there an easier way to achieve the following? var obj = from row

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.