Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Create a Table variables schema from an existing table

Posted on 2003-10-30
4
Medium Priority
?
1,743 Views
Last Modified: 2012-05-04
Hello all,

I need to select some rows from a table in to a table variable. The problem I am having is that I need to define the schema for the table variable, which is essentailly the same as the table that I am selecting from. I was wondering if there was some other way of defining a table variables schema from a source, rather than hard coding it.

So something like
DECLARE @myTable table (SELECT * FROM tMyOtherTable)

would be nice

Also, using my table variable I then select a series of related data based on the pk.

So i'm doing

Select
        tBooleanValues.*
FROM
        tBooleanValue
        JOIN tBooleanValue ON tFieldValue.FieldValueID = tBooleanValue.FieldValueID
WHERE
        tFieldValue.ContainerID IN (SELECT ID FROM @myTable)

this several times. The only thing that changes is the tBooleanValue becomes tTextValue or tDateValue etc. I was wondering if there was some way I could put this in a loop and use an alias for the table that I want to select from.

Cheers
0
Comment
Question by:worldspawn
[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
  • 2
4 Comments
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 1050 total points
ID: 9654372
the short answer is no SQL Server doesn't support a LIKE syntax for create table
so you'll have to specify the full details...

you can of course get enterprise manager to script the table ddl for you and them cut and paste it into your procedure....
EM -- select table -- right click , all tasks , script generation...

similarly no you can easily substitute the table names
(well you could with dynamic sql but thats over kill and has its own performance / security overheads...)


i would however
replace the containerid in (select id ...

with
EXISTS (Select [id] from @mytable where [id] = tFieldvalue.Containerid)

which should be quicker.
0
 
LVL 1

Author Comment

by:worldspawn
ID: 9654422
well that was pretty much the answer I was expecting... thanx for the tip on the "EXISTS"

The point of not using the hard coded table structure was so that any SP's that do it can cope with a table structure change without falling over.

alas it was not meant to be
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 9654478
Not 100% sure what your after, but would this do the trick??

DECLARE @sql NVARCHAR
SET @sql = 'SELECT * FROM tMyOtherTable'

EXEC sp_executesql @sql
0
 
LVL 1

Author Comment

by:worldspawn
ID: 9654480
not really. no.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

597 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