declare @e varchar(8000)
select @e=''
select @e = @e+'case when isnumeric(['''+name+'''])=1 then '+name+' else null end ,'
from syscolumns where id=object_id('table')if len(@e) >0 
select @e=substring(@e,1,len(@e)-1)print @e

解决方案 »

  1.   

    declare @e varchar(8000)
    select @e=''
    select @e = @e+'case when isnumeric(['+name+'])=1 then ['+name+'] else null end ,'
    from syscolumns where id=object_id('table')if len(@e) >0 
    select @e=substring(@e,1,len(@e)-1)print @e
      

  2.   

    试了一个上午,没有试成功,发现在查询分析器print后得到的@e内容不完整,只能显示到4000左右的字符,比如最后的关键字应该是end的,print后只显示到en,后面那个d没有,我重新设置变量值来测试可以显示8000,这个是什么原因呢?
      

  3.   

    那估计是因为你里面有中文
    declare @t sysname
    declare #c cursor for select name from syscolumns where id=object_id('table')
    open #c 
    fetch next from #c into @t
    while @@fetch_status=0
    begin
    print @t
    fetch next from #c into @t
    end
    close #c
    deallocate #c
      

  4.   

    然后再去复制粘贴吧,使用多个 varchar变量可以突破 8000 限制 不过代码就多了 懒得写