动态执行变量表名 表名赋值,该如何处理

动态执行变量表名 表名赋值if object_id(tempdb..#t) is not null drop table #tcreate table #t (name

动态执行变量表名 表名赋值
if object_id('tempdb..#t') is not null drop table #t 
create table #t (name varchar(10)) 
insert into #t select 'F201101' 
insert into #t select 'F201102' 
insert into #t select 'F201103' 
insert into #t select 'F201104' 
insert into #t select 'F201105' 
insert into #t select 'F201106' 
insert into #t select 'F201107' 
insert into #t select 'F201108' 
insert into #t select 'F201109' 
insert into #t select 'F201110' 
insert into #t select 'F201111' 
insert into #t select 'F201112'
select * from #t

declare @table varchar(50)
declare @sql varchar(8000)
set @table='select name from #t '这个怎么赋值啊
set @sql='select * from '+@table+''
exec (@sql)

[解决办法]

SQL code
declare @table varchar(50)declare @sql varchar(8000)select @sql=isnull(@sql+' union all ','')+'select * from ['+name+']'from (select distinct name from #t) t--print @sqlexec (@sql)