--示例--示例数据 create table 表一(A int,B int,C int,D int) insert 表一 select 1,2,3,4create table 表二(a varchar(10),b int) insert 表二 select 'E',5 union all select 'F',6 union all select 'G',7 union all select 'H',8 go--查询处理 declare @s nvarchar(4000) set @s='' select @s=@s+','+quotename(a)+'='+rtrim(b) from 表二 exec('select *'+@s+' from 表一') go--删除测试 drop table 表一,表二/*--结果A B C D E F G H --- --- --- --- --- --- --- ------- 1 2 3 4 5 6 7 8 --*/
create table 表一(A int,B int,C int,D int)
insert 表一 select 1,2,3,4create table 表二(a varchar(10),b int)
insert 表二 select 'E',5
union all select 'F',6
union all select 'G',7
union all select 'H',8
go--查询处理
declare @s nvarchar(4000)
set @s=''
select @s=@s+','+quotename(a)+'='+rtrim(b) from 表二
exec('select *'+@s+' from 表一')
go--删除测试
drop table 表一,表二/*--结果A B C D E F G H
--- --- --- --- --- --- --- -------
1 2 3 4 5 6 7 8
--*/