求一 SQL语句
我想在查询中根据不同的情况取不同的库表
例如:
declare @ab varchar(1)
select @ab= case when bh = '1' then 'table1' else 'table2'
select bh,mc from @ab
/*我这么写 执行的时候提示 没有@ab 表*/
declare @bh varchar(1000)='1'
declare @ab varchar(1000)
select @ab= case when @bh = '1' then 'table1' else 'table2' end
Exec('select bh,mc from '+ @ab)
declare @ab varchar(10),@tsql varchar(6000)
select @ab=case when bh='1' then 'table1' else 'table2' end
select @tsql='select bh,mc from '+@ab
exec(@tsql)