Unanswered: How to use an external table variable within dynamic SQL?
I am writing some dynamic sql for some internal adhoc queries, and ran into issues when trying to call a table variable within the dynamic sql that was declared outside of it. Here is a simplified example:
DECLARE @t table (num int)
INSERT INTO @t VALUES (1),(2),(3)
DECLARE @sql nvarchar(MAX) = N'SELECT * FROM @t'
The throws a "Must declare table variable @t" error. Thoughts? (using 2008 R2)
EDIT: I know I could use a temp table instead of a table variable, but now I am curious...