日期:2014-05-18 浏览次数:20417 次
declare @sql nvarchar(1000) select @sql='' select @sql=@sql+'select title from '+form_id+ ' where filter = '+form_filter+ 'union all ' from flow exec(@sql)
------解决方案--------------------
try this,
create table flow (id int, name varchar(5), descr varchar(5), form_id varchar(5), form_filter varchar(10)) insert into flow select 1,'n_1','d_1','tab1','23231232' create table form_tab1 (form_filter varchar(10),title varchar(10)) insert into form_tab1 select '23231232','t_1' create table #t(id int,title varchar(10)) declare ap cursor forward_only for select 'select '+rtrim(id)+', title from form_'+form_id+' where form_filter='''+form_filter+''' ' tsql from flow declare @sql varchar(6000) open ap fetch next from ap into @sql while(@@fetch_status<>-1) begin insert into #t(id,title) exec(@sql) fetch next from ap into @sql end close ap deallocate ap select a.id,a.name,a.descr,b.title from flow a inner join #t b on a.id=b.id /* id name descr title ----------- ----- ----- ---------- 1 n_1 d_1 t_1 (1 row(s) affected) */