Solved

Create a Table variables schema from an existing table

Posted on 2003-10-30
4
1,735 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
not really. no.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

763 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

6 Experts available now in Live!

Get 1:1 Help Now