我们正在做这样的事情
create view v1
as
select 'tab1' as table_name , * from table1
union All
select 'tab2' as table_name , * from table2
union All
select 'tab3' as table_name , * from table3
然后在SP中,
declare @var = function_gettablename()
select * from v1 where table_name = @var and datetoload = <ActualDate>
function_gettablename() = gives the table name based on parameters
datetoload =非聚集索引列,在所有表上都是mutuallt独占。即一个日期仅在一个表格中可用
现在问题在于执行计划:使用@var使视图查询所有三个表而不是所需的表。
有没有办法应用过滤器然后应用union all,而不是运行union all然后应用过滤器。
由于 AB
答案 0 :(得分:0)
请检查一下 - :
DECLARE @Var VARCHAR(MAX)
SET @Var = function_gettablename()
DECLARE @Sql VARCHAR(MAX)
DECLARE @ActualDate DATETIME
SET @ActualDate ='2013/2/14'
SET @sql = 'SELECT tab1 as '+@Var+' , * FROM table1
WHERE dateload ='+@ActualDate+ ' UNION ALL
SELECT tab2 as '+@Var+' , * FROM table1
WHERE dateload ='+@ActualDate+' UNION ALL
SELECT tab3 as '+@Var+',* FROM table1
WHERE dateload ='+@ActualDate
Exec(@Sql)