?
Solved

Create temp table using dynamic sql

Posted on 2016-08-23
6
Medium Priority
?
100 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 44

Assisted Solution

by:zephyr_hex (Megan)
zephyr_hex (Megan) earned 500 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 66

Assisted Solution

by:Jim Horn
Jim Horn earned 500 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 44

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 66

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 70

Accepted Solution

by:
Scott Pletcher earned 1000 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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

830 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