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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:18:19+00:00 2026-05-26T19:18:19+00:00

I having a problem with my current situation, I searched through any solution on

  • 0

I having a problem with my current situation, I searched through any solution on the net but still I can’t get it.

Here the question:

I have a bunch of SQL statements that need to be executed in a stored procedure by using cursor inside, each of the statements is performing an insert by selecting from a different database as well as table.

For example:

INSERT INTO Database1.Table1(column1, column2, column3)
   SELECT column1,column2, column3
   FROM Database2.Table2
   WHERE --Some Condition

and maybe another executed SQL statement is like this

INSERT INTO Database1.Table3(column1, column2, column3, column4)
   SELECT column1, column2, column3, column4
   FROM Database3.Table3
   WHERE --Some Condition

Okay, basically my process is like this

Execute Sql to insert into temp Tables --> Insert into a permanent Table from Temp Tables

From above two SQL statements, my executed result from database2 or 3 or may be 4,5 and etc. I will goes to my database1 for permanent store. In more summary way of telling that’s is, I just want to made another copy of data that get from different source of database and store into my local database to do some further processing.

My main problem is my person in charge (or manager) told me to trow all executed result into a TEMP table or #Table before execute into the permanent.

Something like this:

INSERT INTO #Table3(column1, column2, column3, column4)
   SELECT column1, column2, column3, column4
   FROM Database3.Table3
   WHERE --Some Condition

I went through some research on #Temp tables and I found it most of them is creating with ‘FIX’ column such as

CREATE TABLE #Table
(
    column1 VARCHAR(10),
    column2 VARCHAR(10),
    column3 VARCHAR(10)
)

PROBLEM: is there anyway to create it with dynamic columns? More detail way of asking, is there anyway to insert by select into a #Temp table without prefix the column? Because that is impossible for me to create a bunch of temp table for each of executed SQL.

Thank you

PS 1: I am quite a newbie to SQL Server, please don’t hesitate to voice out my mistake or error. We all learn from mistakes.

PS 2: Sorry for my poor English level, I tried my best to elaborate it more clearly.

Regards:

LiangCk

  • 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-26T19:18:20+00:00Added an answer on May 26, 2026 at 7:18 pm

    You say that you want something like

    
    
    INSERT INTO #Table3(coloum1,coloum2,coloum3,coloum4)
    Select coloum1,coloum2,coloum3,coloum4
    FROM Database3.Table3
    WHERE --Some Condition
    
    
    

    without having to create the temp table with fixed columns first.

    This would work:

    
    
    Select coloum1,coloum2,coloum3,coloum4
    INTO #Table3
    FROM Database3.Table3
    WHERE --Some Condition
    
    
    

    You don’t have to create the temp table or specify the columns first, just select into the temp table and it will be created on the fly.

    It sounds like you want to do something more than this… I can’t quite figure out what that is, but it sounds like maybe selecting all of your data from various tables into a single temp table (I don’t know why you would want to do this)… If that’s the case then UNION or UNION ALL should work. You can still use these with the dynamically created temp table.

    
    
    Select coloum1,coloum2,coloum3,coloum4
    INTO #Table3
    FROM Database3.Table3
    WHERE --Some Condition
    
    UNION
    
    Select column1, column2, column3, null
    FROM Database1.Table1
    WHERE --Some condition
    
    
    

    The null above is just to give the 2nd select the same number of columns as the first (I used both selects from your post); this is a requirement for UNION.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having trouble finding a solution for my situation here. Sorry if this has been
Having a problem here.... I can't seem to find a good way to 'add'
I'm having a problem with configuring CC.Net. My current setup for the project looks
I'm having problem with Rails plugin attachment_fu . On every upload, I get validation
I'm having a problem refreshing text on a timer. I am displaying the current
Current I having problem on using CSS position:fixed in my IE 7, Noticed it
I'm having trouble working with the datetime format. Current situation: I'm having a database
I am trying to perform a lightweight migration, but I am having a problem
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
We're having problem with a huge number of legacy stored procedures at work. Do

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.