Solved

Create temp table using dynamic sql

Posted on 2016-08-23
6
60 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delete duplicate data from two columns in a SQL Table 19 42
Database maintenance 36 98
SQL Pivot table 2 42
SQL Server Trigger 8 33
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

685 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