Hi all,
I have been attempting to construct some dynamic SQL using a table name as a parameter, and call it using sp_executesql, in SQL Server 2005.
According to all of the searches and articles I have read today, the following should work:
DECLARE @rownum INT
DECLARE @tabNam NVARCHAR(100)
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)
SET @rownum = 35
SET @tabNam = 'AdminScriptHistory'
SELECT @SQLString = N'SELECT * FROM @table where executionorder=@row'
SET @ParmDefinition = N'@table VARCHAR(100), @row INT'
EXEC sp_executesql @SQLString, @ParmDefinition, @tabNam,@rownum
However, it fails with:
Msg 1087, Level 15, State 2, Line 1
Must declare the table variable "@table".
Anybody know why?
Cheers
I have been attempting to construct some dynamic SQL using a table name as a parameter, and call it using sp_executesql, in SQL Server 2005.
According to all of the searches and articles I have read today, the following should work:
DECLARE @rownum INT
DECLARE @tabNam NVARCHAR(100)
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)
SET @rownum = 35
SET @tabNam = 'AdminScriptHistory'
SELECT @SQLString = N'SELECT * FROM @table where executionorder=@row'
SET @ParmDefinition = N'@table VARCHAR(100), @row INT'
EXEC sp_executesql @SQLString, @ParmDefinition, @tabNam,@rownum
However, it fails with:
Msg 1087, Level 15, State 2, Line 1
Must declare the table variable "@table".
Anybody know why?
Cheers