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)
 EXEC with Table variables

Author  Topic 

MRIGUY
Starting Member

17 Posts

Posted - 2001-08-28 : 13:15:55
I created a dynamic sql statement that has a table variable in it. This returns and error that states I must declare the variable.

DECLARE @tbl Table
(fld1 char(10))

exec ('SELECT * from ' + @tbl)
-------
Server: Msg 137, Level 15, State 2, Line 4
Must declare the variable '@tbl'.
[\code]

I looked in BOL and found that the EXEC statement doesn't work well with variables until you use the SET command. This is BOL's example.

[code]
USE Northwind
GO
DECLARE @mycol nvarchar(20)
SET @mycol = 'ContactName'
EXECUTE ('SELECT ' + @mycol + ' FROM Customers')


Any chance table variables will work in a dynamic SQL statement like the one above.

Thanks

   

- Advertisement -