--建立测试环境
Create Table 表(ColID varchar(10),Col1 varchar(10),Col2 varchar(10),Col3 varchar(10))
--插入数据
insert into 表
select '1','abc','1','2.3' union
select '2','other','30','45.12'
--select * from 表
--测试语句
declare @s varchar(8000)
set @s='select * from(select null as ColID,null as 列'
select @s=@s+' union select ColID,'+ name+' from 表' 
from syscolumns where id=object_id('表') and name<>'ColID'
set @s=@s+')a where colid is not null'exec(@s)
 
--删除测试环境
Drop Table 表/*
ColID 列
1 1
1 2.3
1 abc
2 30
2 45.12
2 other
*/

解决方案 »

  1.   

    to zjcxc(邹建)
    表名: Table 列名: Col1,Col2, Col3 保存在另一表中。
      

  2.   

    --建立测试环境
    Create Table tb(ColID int,Col1 varchar(10),Col2 varchar(10),Col3 varchar(10))
    insert tb select 1,'abc'  ,'1' ,'2.3'
    union all select 2,'other','30','45.12'
    go--查询的存储过程
    create proc p_qry
    @tbname sysname,  --查询那个表中的数据
    @ColID int        --ColID列值
    as
    declare @s nvarchar(4000)
    set @s=''
    select @s=@s+N' union all select '+quotename(name,N'''')
    +N','+quotename(name)
    +N' from '+quotename(@tbname)
    +N' where ColID=@ColID'
    from syscolumns 
    where name<>'ColID' and id=object_id(@tbname)
    order by colid
    set @s=N'select fdname='+stuff(@s,1,18,N'')
    exec sp_executesql @s,N'@ColID int',@ColID 
    go--调用
    exec p_qry 'tb',2
    go
    --删除测试
    drop table tb
    drop proc p_qry/*--测试结果fdname Col1       
    ------ ---------- 
    Col1   other
    Col2   30
    Col3   45.12(所影响的行数为 3 行)
    --*/
      

  3.   

    --通用一点--建立测试环境
    Create Table tb(ColID int,Col1 varchar(10),Col2 int,Col3 decimal(10,2))
    insert tb select 1,'abc'  ,1 ,2.30
    union all select 2,'other',30,45.12
    go--查询的存储过程
    create proc p_qry
    @tbname sysname,  --查询那个表中的数据
    @ColID int,       --ColID列值
    @ColID_Fd sysname=N'ColID'   --ColID列名
    as
    declare @s nvarchar(4000)
    set @s=''
    select @s=@s+N' union all select '+quotename(name,N'''')
    +N',Value=cast('+quotename(name)+N' as varchar)'
    +N' from '+quotename(@tbname)
    +N' where '+quotename(@ColID_Fd)+N'=@ColID'
    from syscolumns 
    where name<>@ColID_Fd and id=object_id(@tbname)
    order by colid
    set @s=N'select fdname='+stuff(@s,1,18,N'')
    exec sp_executesql @s,N'@ColID int',@ColID 
    go--调用
    exec p_qry 'tb',2
    exec p_qry 'sysobjects',2,N'id'
    go
    --删除测试
    drop table tb
    drop proc p_qry/*--测试结果fdname Value                          
    ------ ------------------------------ 
    Col1   other
    Col2   30
    Col3   45.12(所影响的行数为 3 行)fdname           Value                          
    ---------------- ------------------------------ 
    name             sysindexes
    xtype            S 
    uid              1
    info             29
    status           -536870907
    base_schema_ver  32
    replinfo         0
    parent_obj       0
    crdate           08  6 2000  1:29AM
    ftcatid          0
    schema_ver       32
    stats_schema_ver 0
    type             S 
    userstat         1
    sysstat          8273
    indexdel         0
    refdate          08  6 2000  1:29AM
    version          0
    deltrig          0
    instrig          0
    updtrig          0
    seltrig          0
    category         2
    cache            0(所影响的行数为 24 行)
    --*/