Solved

Create temp table using dynamic sql

Posted on 2016-08-23
6
77 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 66

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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

627 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