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 2005 Forums
 Transact-SQL (2005)
 Query help

Author  Topic 

srivatsahg
Yak Posting Veteran

71 Posts

Posted - 2009-10-28 : 05:31:37
I have written a query to create a table having dynamic columns

Declare @cols varchar(2000)
Declare @query varchar(2000)
Select @cols = COALESCE(@cols + ',['+[header]+'] varchar(10)','['+[header]+'] varchar(10)' from ColumnHeadersTable)

SET @query = '
DECLARE @tempTable TABLE
(
ID uniqueidentifier,
'+@cols+'
)'

EXECUTE(@query)


Though this query executes successfully,
After this if i refer the @tempTable i get error saying
Must declare the table variable "@tempTable".

Any ideas whats happening here ??

Regards
Srivatsa

YellowBug
Aged Yak Warrior

616 Posts

Posted - 2009-10-28 : 05:44:06
@tempTable exists in the scope of the dynamic query only - that's where it is declared.
Go to Top of Page
   

- Advertisement -