To do this, you will need to use dynamic SQL (provided that you want to scale it to more than two tables), which will work, but is suboptimal, since SQL will not generate statistics for it and it will be more difficult to optimize the query.
declare @table sysname
declare @SQL varchar(1000)
set @table = 'MyTable'
SET @SQL='SELECT orderID, Quantity FROM ' + QUOTENAME(@table) + ' WHERE filled=0'
exec sp_executesql @SQL
or in a stored procedure:
CREATE PROCEDURE p_ConditionalSelect @table sysname
as
declare @SQL varchar(1000)
set @table = 'MyTable'
SET @SQL='SELECT orderID, Quantity FROM ' + QUOTENAME(@table) + ' WHERE filled=0'
exec sp_executesql @SQL
source
share