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
 SQL Server Development (2000)
 Create a Temp Table using another table rowset

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-11-11 : 07:45:50
Paul writes "Hi, Thanks in advance to anyone answering this.

I would like to create a table eg. #Temp that derives its field names from the predicate "select Description from tblRefData"

Has anyone tried this before, I have tried using a cursor to drive the field naming, but I seem to miss the point somewhere.

Cheers

Paul"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-11-11 : 07:52:06
DECLARE @sql varchar(8000)
SET @sql='CREATE TABLE myTable ('
SELECT @sql=@sql + quotename(Description) + ' varchar(100), ' FROM tblRefData
SELECT @sql=Left(@sql, Len(@sql)-2) + ')'
SELECT @sql
--EXEC(@sql)
--uncomment this line to create the table

That should do it, but all of the column names will be the same data type (varchar(100)). If you have datatype information in the table then you can substitute the column names in the appropriate places.
Go to Top of Page
   

- Advertisement -