Is there anyway i can do something like
Is there anyway i can get a variable into a FROM clause. Like this
SELECT * FROM myGetTableFunc(@tableName)
SELECT * FROM EXEC('SELECT * FROM ' + @tableName)
SELECT * INTO #myUndeclaredTable FROM @tableName
I think you get the point.
Basicly i will have a few tables with shared common columns like a sorting key etc. In a stored procedure i use these shared columns to work the table and join some stuff and then return it.
But currently I will need a new stored procedure for each table because I can't use a variable in the FROM clause. Is my only solution to turn the whole thing into dynamic SQL?
Things such as OPENQUERY
OPENROWSET
will not be possible.
I thought about something like
EXEC ('SELECT * INTO #myTempTable FROM ' + @tableName)
SELECT * FROM #myTempTable
But #myTempTable
don't seem to be visible even though i thought it had scope for the entire session.
Thank you for any help!