Solved

Create temp table using dynamic sql

Posted on 2016-08-23
6
53 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 43

Assisted Solution

by:zephyr_hex (Megan)
zephyr_hex (Megan) 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 43

Expert Comment

by:zephyr_hex (Megan)
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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:
Scott Pletcher 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

809 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