We have 2 tables with identical structure and based on a variable I want to choose which table to select on with out having to write 2 queries in my procedure.
Is this possible?
I tried
declare @table int
set @table = 1
Select orderID, Quantity
from case when @table = 1 then tblOrders else tblSubscriptionOrders end
where filled = 0
But that did not work
You would need to use dynamic SQL for this (assuming you want to scale it to more than just 2 tables), which would work but is suboptimal as SQL will not generate statistics for it and have a harder time optimizing the query.
or, in a stored procedure: