Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Dynamic Table Creation

Author  Topic 

TimK
Starting Member

3 Posts

Posted - 2003-02-25 : 11:38:03
Hi,

Is it possible to create a table based on the results of a stored proc? I know I can do this from a table e.g. select * into #temp from myTable but can I do this select * into #temp exec spMyStoredproc

Thanks
Tim

nr
SQLTeam MVY

12543 Posts

Posted - 2003-02-25 : 11:50:53
I always wonder why people want to do this - sounds like dubious design.
Yoyu can do it using openquery defining a linked server pointing at the local server

select *
into #a
from openqquery(self, 'exec spMyStoredproc')

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-02-25 : 11:52:10
Not with that syntax. You'd have to modify the stored procedure to run the SELECT...INTO instead of SELECT. Also, a temp table would not have scope outside of the sproc, once it finishes the table will be dropped unless you use a global temp table.

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-02-25 : 13:11:16
I use the following to get information about a directory:

Insert Into Ledger_Folder exec master..xp_cmdshell 'Dir d:\Data\Tax\SmartStreamExtracts\*.*'

Hope this helps

Brett

8-)

Go to Top of Page

TimK
Starting Member

3 Posts

Posted - 2003-02-26 : 04:31:27
quote:

I always wonder why people want to do this - sounds like dubious design.



I have a report that uses data in a joined lookup table as the coloumn headings. This lookup table's data can be added to which would mean I would have to keep updating the proc.


Go to Top of Page

Bambola
Posting Yak Master

103 Posts

Posted - 2003-02-26 : 04:55:51
You can do one of the following

insert into #temp
exec spMyStoredproc

Or, if you can re-write spMyStoredproc as a table-function or inline-function, you can do

SELECT * FROM dbo.myTableFunction(param list...)

like this you can skip the #temp table. If you are using SQL 2000, you could also replace the #temp table with a @table_datatype.

Bambola.

Go to Top of Page

TimK
Starting Member

3 Posts

Posted - 2003-02-26 : 06:17:47
quote:

You can do one of the following

insert into #temp
exec spMyStoredproc



But this assumes you know how many columns to define when creating the temp table. I could potentially have any number of columns depending on data in the lookup table.

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-02-26 : 07:30:56
Use a cross tab query:

http://www.sqlteam.com/item.asp?ItemID=2955

Go to Top of Page
   

- Advertisement -