我现在有个行列转换后的动态表,不知道它的具体列,我将执行的结果集放在了全局临时表A中,然后A再和其他表连接聚合,现在我想要A中某列外的其他列,能实现吗?

解决方案 »

  1.   

    declare @col varchar(1000)
    set @col=''
    select @col=@col+','+name from syscolumns where id=object_id('表名') and name<>'排除的字段名' order by colid
    set @col=stuff(@col,1,1,'')
    exec('select '+@col+' from 表名')
      

  2.   

    declare @col varchar(1000)
    set @col=''
    select @col=@col+','+name from syscolumns where id=object_id('表名') and name<>'排除的字段名' order by colid
    set @col=stuff(@col,1,1,'')
    exec('select '+@col+' from 表名')
      

  3.   

    谢谢,但现在存在的问题是:select * from ##monthReport 
    declare @col varchar(1000)
    set @col=''
    select @col=@col+','+name from syscolumns where id=object_id('##monthReport') and name<>'shop' order by colid
    set @col=stuff(@col,1,1,'')
    print @col 我可以select 出数据,但在syscolumns 中没有##monthReport 
    我是菜鸟,请教
      

  4.   

    ##monthReport  
    你把这个换成你库里的表名 试试
      

  5.   

    换成库里的可以啊。
    难道临时表的列信息不是存储在syscolumns里吗?
    呃~~
      

  6.   

    sysindexes里面夏的时候,是没有##的,所以不行。
      

  7.   

    sysindexes里面存的时候,是没有##的,而是一个后缀,所以不行。
      

  8.   


    ## 在tempdb 里,你要去那才能查到
      

  9.   


    谢谢,但现在需要的是##monthReport的列信息,然后组合成一个查询串
      

  10.   

    create table ##tb
    (
    col1 int,
    col2 int,
    col3 int,
    col4 int,
    col5 int
    )insert into ##tb
    select 1,2,3,4,5 union all
    select 2,3,4,5,6 union all
    select 3,4,5,6,7 union all
    select 4,5,6,7,8use tempdb
    declare @col varchar(1000)
    set @col=''
    select @col=@col+','+name from sys.columns where object_id=object_id('##tb') and name<>'col2' order by column_id
    set @col=stuff(@col,1,1,'')
    --print @col
    exec('select '+@col+' from ##tb')col1        col3        col4        col5
    ----------- ----------- ----------- -----------
    1           3           4           5
    2           4           5           6
    3           5           6           7
    4           6           7           8(4 row(s) affected)
      

  11.   

    谢谢,在前面加 use tempdb 后可以了
    使用tempdb下的sys.columns视图。