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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:31:25+00:00 2026-06-17T06:31:25+00:00

I create temp tables quite often in SQL and I am looking into a

  • 0

I create temp tables quite often in SQL and I am looking into a way to generate the column names and datatypes automatically for the table definition so I don’t have to look them all up everytime.

For example I run:

SELECT CustomerID
  ClientID,
  FirstName
  LastName
INTO #Test
From dbo.Customer

To initially setup the temp table with the appropriate columns and data I need. Once I get that all done, I then go back in and take out the INTO statement and write the following:

CREATE TABLE #Test
(
...
...
);

I want to find a way to auto generate the column names and datatypes from the initial creation of the temp table. Right now, since I am initially inserting into an automatically created temp table, I use this:

EXEC tempdb..sp_help '#Test';

This gives me everything I need without having to look all the column datatypes up, but I wanted to know if there was a way to just auto gen the column names off of something like this. So the auto gen would generate:

CustomerID int,
ClientID int,
FirstName varchar(50),
LastName varchar(50)

This would allow me to just copy and paste this into my create table statement.

  • 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-17T06:31:26+00:00Added an answer on June 17, 2026 at 6:31 am

    this might give you a start:

    DECLARE @viewname VARCHAR(50);
    SET @viewname ='tableorviewname';
    SELECT  c.name + ' '+ t.name + 
    case t.name  
    WHEN 'varchar' THEN '('+CAST(c.max_length AS VARCHAR(3) )+'),'
    ELSE ',' 
    end
    FROM sys.columns c
    INNER JOIN sys.types AS t ON c.system_type_id = t.system_type_id
    WHERE object_id = (SELECT object_id from sys.objects where name = @viewname)
    ORDER BY c.column_id
    

    EDIT: TEMP TABLES:

    temp tables are slightly different, for instance this works in sql 2008 for a temp table named #tv_source

    DECLARE @viewortablename VARCHAR(50);
    SET @viewortablename ='tempdb..#tv_source';
    SELECT  c.name + ' '+ t.name + 
    case t.name  
    WHEN 'varchar' THEN '('+CAST(c.max_length AS VARCHAR(3) )+'),'
    ELSE ',' 
    end
    FROM tempdb.sys.columns c
    INNER JOIN sys.types AS t ON c.system_type_id = t.system_type_id
    WHERE object_id =  object_id(@viewortablename)
    ORDER BY c.column_id
    

    NOTES: this gives a comma separated list, but did NOT attempt to remove that last comma, it gives only a list, which you would likely want to put on a string and manipulate, etc. then use as a dynamic sql or somthing. Still, it should give you a start on what you wish to do.

    NOTE for to others, sql 2000 would not display the lengths properly for instance on a varchar(45), it would just list the varchar part and I did not attempt to rework that for this question.

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

Sidebar

Related Questions

In SQL Server, there's no way to create a temp table on the fly
What is the difference between: CREATE TABLE #temp ( [ID] INT) INSERT INTO #temp
I seem to be running into a problem with temp table garbage collection: CREATE
Two temp tables are created and then loaded...Here's the schema. Create table #SH ([date]
I have written a DB2 query to do the following: Create a temp table
How can i get result My Dream Table Result CREATE TABLE #temp( [count] int
I have temp table #xml that have 2 columns: CREATE TABLE #xml ( id
CREATE TABLE #TEMPTABLE ( requestTime datetime2, NoOfSessions integer ) INSERT INTO #TEMPTABLE select convert(nvarchar(16),
When using temp tables in SQL Server stored procs, is the preferred practice to;
I am trying to create a sybase stored procedure which references a temp table,

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.