Why I can't use template table in dynamic query SQL SERVER 2005

Posted by StuffHappens on Stack Overflow See other posts from Stack Overflow or by StuffHappens
Published on 2010-06-08T11:59:30Z Indexed on 2010/06/08 12:02 UTC
Read the original article Hit count: 219

Filed under:
|
|

Hello!
I have the following t-sql code which generates an error

Declare @table TABLE
(
    ID1 int,
    ID2 int
)

INSERT INTO @table values(1, 1); INSERT INTO @table values(2, 2); INSERT INTO @table values(3, 3);

DECLARE @field varchar(50); SET @field = 'ID1'

DECLARE @query varchar(MAX); SET @query = 'SELECT * FROM @table WHERE ' + @field + ' = 1' EXEC (@query)

The error is Must declare the table variable "@table".
What's wrong with the query. How to fix it?

© Stack Overflow or respective owner

Related posts about sql-server-2005

Related posts about tsql