我要把一个表(y0335)中的列名+列值引出,用什么样的方法好?
[email protected]:列名,@colv:该列值,@text:各列名+列值
declare @i int
set @i=1
declare @text nvarchar(1000),@sql varchar(1000)
set @text= ' '
while @i <16 --假定为16列
begin
declare @coln nvarchar(30),@colv varchar(10)
--取出列名
set @coln=(select col_name(object_id( 'y0335 '),@i) )
--如果列名不空,取列值,并进行连接
if @coln <> ' '
begin
select (@coln)
--select (@colv)
set @sql = 'select '+ @coln + ' from y0335 where sid= '+char(39)+ '20033502 '+char(39)
select (@sql)
exec (@sql)
set @colv = exec (@sql) --该语句怎么写??
if @colv <> ' '
begin
set @text = @text + '| ' [email protected]+ ', '[email protected]
select (@text)
end
end
set @[email protected]+1
end
select @text
------解决方案--------------------
declare @i int
set @i=1
declare @text nvarchar(1000),@sql nvarchar(1000)
set @text= ' '
while @i <16 --假定为16列
begin
declare @coln nvarchar(30),@colv varchar(10)
--取出列名
set @coln=(select col_name(object_id( 'y0335 '),@i))
--如果列名不空,取列值,并进行连接
if @coln <> ' '
begin
if object_id( 'gggg ') is not null
drop table gggg
set @sql = 'select ltrim( '+ @coln + ') aaaa into gggg from y0335 where a2= '+char(39)+ '20033502 '+char(39)
exec (@sql)
select @colv=aaaa from gggg
if @colv <> ' '
begin
set @text = @text + '| ' [email protected]+ ', '[email protected]
end
end
set @[email protected]+1
end
select @text