Solved

Create temp table using dynamic sql

Posted on 2016-08-23
6
38 Views
Last Modified: 2016-08-26
I have a stored procedure in which I need to create a single temp table based on one of several views

Each of the views has a differing amount of fields

I will use this table to turn around and  generate a csv file

Is there a way to put the view name into a variable and use that variable value to create the temp table?
0
Comment
Question by:johnnyg123
6 Comments
 
LVL 42

Assisted Solution

by:zephyr_hex
zephyr_hex earned 125 total points
ID: 41767677
You could just select all columns into the temp table:
SELECT *
INTO #tmp_table
FROM MyView

Open in new window

0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 125 total points
ID: 41767711
>create a single temp table based on one of several views each of the views has a differing amount of fields
Just to make the obvious statement, YOU ARE PLAYING WITH FIRE here as changes in columns will cause the creation of a single set to fail.

Aside from that, you can always UNION views together creating dummy columns where you need them.  Something like.
SELECT goo, foo, boo, '' as yabba, '' as dabba, '' as doo
FROM cute_names
UNION
SELECT '', '', '', yabba, dabba, doo
FROM fred_flintstone_sayings

Open in new window

0
 
LVL 42

Expert Comment

by:zephyr_hex
ID: 41767716
Wait.  Is the OP asking for a dynamic way to create a temp table based on 1 view ?   That's how I read it.  If that's not the case, then please disregard my earlier answer.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 65

Expert Comment

by:Jim Horn
ID: 41767720
Hmm .. looks like we'll need a clarification, as I interpreted it as multiple views, which after a second reading may not be correct.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
ID: 41769111
It would be vastly easier to use a non-temp table, even if you create it in the tempdb db.

The difficulty with a temp table is that a temp table created in dynamic SQL would be automatically deallocated when the dynamic SQL ends, since that SQL runs as a separate batch.

But you could generate a unique name for a "real" table in tempdb -- such as using YYYYMMDD_<guid>, minus the dashes in the guid -- and use that for processing to the csv, then delete the table explicitly yourself when you're done.  That name pattern would allow you to delete "left-over" tables later as well.
0
 

Author Closing Comment

by:johnnyg123
ID: 41772047
Thanks for all the posts!

After rereading my question I realized I was indeed unclear

I wanted to give at least some points to all posts

Ultimately scott's suggestion is the one I went with so I give him the most
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now