有一个表t_wzwzdm  
01
02
03
aa
bb我想把wzdm列的所有值,组合成一个字符串:'01/02/03/aa/bb',能用一个SQL语句来实现吗?不要用存储过程。

解决方案 »

  1.   

    declare @n nvarchar(4000)
    set @n = ''
    select @n = @n + wzdm + '/'
    from t_wz
      

  2.   

    select stuff((select '/'+wzdm from t_wz for xml path('')),1,1,'')
      

  3.   

    if object_id('[t_wz]') is not null drop table [t_wz]
    go
    create table [t_wz] (wzdm nvarchar(4))
    insert into [t_wz]
    select '01' union all
    select '02' union all
    select '03' union all
    select 'aa' union all
    select 'bb'
    select distinct [values]=stuff((select '/'+wzdm from t_wz t  for xml path('')), 1, 1, '') 
    from t_wz 
    --01/02/03/aa/bb
      

  4.   

    抱歉,忘了告诉大家我用的是SQL2000了。lhqdyy9:你的语句不行啊?szm341,OrchidCat :系统对xml path报错
      

  5.   

    那你自己写个标量函数吧,1L的方法是对的,只是没写输出
    declare @n nvarchar(4000)
    set @n = ''
    select @n = @n + wzdm + '/'
    from t_wz
    select @n
      

  6.   

    这个可以的,最后再加一句 Select @n
      

  7.   


    2000 用函数,或者是1楼的语句就OKdeclare @n nvarchar(4000)
    set @n = ''
    select @n = @n + wzdm + '/'
    from t_wz
    select @n