Solved

Create a Table variables schema from an existing table

Posted on 2003-10-30
4
1,736 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
  • 2
4 Comments
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 350 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now