用动态SQL实现,create table fld
(sn int,
pyname varchar(10),
syname varchar(10))insert into fld
select 1,'id','ID号' union all
select 2,'mc','名称' union all
select 3,'gg','规格' union all
select 4,'dw','单位'
declare @tsql varchar(6000)select @tsql=isnull(@tsql+',','')
+'a.'+pyname+' as '+syname
from fld
select @tsql='select '+@tsql
+' from sp a,mc b,ghs c where a.id=b.id and a.id=c.id'-- 打印
print @tsql
/*
select a.id as ID号,a.mc as 名称,a.gg as 规格,a.dw as 单位 from sp a,mc b,ghs c where a.id=b.id and a.id=c.id
*/-- 执行
exec(@tsql)
(sn int,
pyname varchar(10),
syname varchar(10))insert into fld
select 1,'id','ID号' union all
select 2,'mc','名称' union all
select 3,'gg','规格' union all
select 4,'dw','单位'
declare @tsql varchar(6000)select @tsql=isnull(@tsql+',','')
+'a.'+pyname+' as '+syname
from fld
select @tsql='select '+@tsql
+' from sp a,mc b,ghs c where a.id=b.id and a.id=c.id'-- 打印
print @tsql
/*
select a.id as ID号,a.mc as 名称,a.gg as 规格,a.dw as 单位 from sp a,mc b,ghs c where a.id=b.id and a.id=c.id
*/-- 执行
exec(@tsql)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货