I have a need for a single select statement that runs another select statement within its from clause and the result (there's only one record) is the table name of the outer select.
For this purpose, I cannot use a stored procedure, exec, or @ variables.
Also, the query file must start with 'select' and contain only that statement.
I know there are better ways to accomplish the end result, however the vendor's wizard that the query must run through sniffs for anything other than a single select statement. Its frustrating, but that's all we have to work with.
Here is an example of what I am trying to do:
select
plan.TransactionID,
plan.PlanName,
sum(plan.Value) as Rate
from
(select 'dbo._Result' + ltrim(str(CalculationID)) from dbo.Calculation where Name =
'TPRL Transaction Plan Rates'
) Plan
Of course, this does not work. However, neither does this:
select
plan.TransactionID,
plan.PlanName,
sum(plan.Value) as Rate
from
exec(select 'dbo._Result' + ltrim(str(CalculationID)) from dbo.Calculation where Name = 'TPRL Transaction Plan Rates') Plan
Even though, this by itself does pull back the table I want into a result set:
exec(select 'dbo._Table' + ltrim(str(CalculationID)) from dbo.Calculation where Name = 'TPRL Transaction Plan Rates')
All I would need for it to do is treat the result as a literal (as in, macro substitution) in the from above, but it doesn't work that way.
Is there a way in tsql to do this?