首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-04-21 
动态执行变量表名 表名赋值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) 

热点排行