Solved

Create a Table variables schema from an existing table

Posted on 2003-10-30
4
1,738 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

829 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