I'm struggling with a dynamic query in tSQL to be used with Excel ODBC.
SQL Server 2016 via ODBC Driver 11 for SQL Server
I found this article but it only uses the table name stored in a parameter, I'd need the actual table to be within parameter: Simple dynamic TSQL query syntax
declare @t table(value1 int, value2 nvarchar(1024))
insert @t SELECT 1 as value1, 'test' as value2
declare @where nvarchar(max)
declare @query nvarchar(max)
declare @sql nvarchar(max)
set @where = ' WHERE value1 = 1'
set @query = 'Select * from @t'
set @sql = @query + @where
EXEC(@sql)
This results in the error message Must declare the table variable "@t"
Unfortunately I can't use temporary tables as the connector doesn't support temp tables.
My original query is far more complex and contains 6 different parameters all being injected at different points into the query and 2 table-parameters that hold temp results
Thanks in advance
