原图:
效果图:
sql代码:
declare @s int,
@i int,
@sql nvarchar(500),
@column1 nvarchar(20),
@column2 nvarchar(20),
@column3 nvarchar(20)
--已知的三个字段
set @column1 = 'code2'
set @column2 = 'code3'
set @column3 = 'show'
--得到表的总字段数
select @s = count(*)
from syscolumns as A
left join sysobjects as B on A.id = B.id
where B.name = 'test' --‘test’为表名
--得到表test的所有字段名
set @sql = ''
if @s>2
begin
set @i = 1
while exists(select * from syscolumns as A left join sysobjects as B on A.id = B.id where
B.name = 'test' and A.colorder = @i)
begin
select @sql = @sql + ',' + A.name from syscolumns as A left join sysobjects as B
on A.id = B.id where B.name = 'test' and A.colorder = @i
set @i = @i + 1
end
end
--去掉已经的三个列
select @sql = replace(@sql,','+@column1+',',',')
select @sql = replace(@sql,','+@column2+',',',')
select @sql = replace(@sql,','+@column3+',',',')
select @sql ='select '+ @column1 + ',' + @column2 + ',' + @column3 + @sql + ' from test' --拼sql语句
exec (@sql)
将已经的三个列显示在前面,后面显示其它列。