declare @t table(col int)
insert @t select 1
union all select 2
union all select 3
declare @s varchar(100)
set @s=''
select @s=@s+' '+cast(col as varchar) from @t
select @s
你看看这个 还有 在邹建出的那本书上有合并行的额

解决方案 »

  1.   

    create table temptbl(
      ordercode varchar(2),
      productcode varchar(20),     
      slipcode  varchar(20),
      updatetime datetime 
     )
    Insert into temptbl
    select 'AA','7841001111301','585072','2005-12-22 00:00:00.000'
    union select 'AA','7841001121027','585072','2005-12-22 00:00:00.000'
    union select 'CA','7841001121027','585072','2005-12-22 00:00:00.000'
    union select 'AA','7841001121034','585072','2005-12-22 00:00:00.000'
    union select 'CA','7841001121034','585072','2005-12-22 00:00:00.000'
    union select 'FA','7841001121034','585072','2005-12-22 00:00:00.000'
    go
    create FUNCTION uf_HBdata(@pcode varchar(20),@Scode varchar(20),@updatetime Datetime)
    RETURNS varchar(8000)
    AS
    BEGIN
    DECLARE @r varchar(8000)
     SET @r=''
       SELECT @r=@r+' '+rtrim(ordercode) 
       FROM temptbl 
       WHERE productcode=@pcode and slipcode=@Scode and updatetime=@updatetime
     RETURN(substring(@r,2,8000))
    END
    go
     
    SELECT ordercode=dbo.uf_HBdata(productcode,slipcode,updatetime) ,
           productcode,slipcode,updatetime
    FROM temptbl 
    GROUP BY productcode,slipcode,updatetime 
    go
      

  2.   

    此类问题见多多了!
    用函数嵌套在SQL命令语句里面实现!
      

  3.   

    如果说AC AA AB这些串连接超出了8000个字符长度,有什么用吗
      

  4.   

    你建一个表。包括
    Text 字段,将合并后字符串每次 8K以内一次次 Update Text吧。我很久没有操作过 Text 字段了,如何 update Text 字段我忘了。我试验了一下,基本可以满足你的要求
    declare @t text
    set @t = replicate('abc',3000)
    create table t ( t1 text)
    insert into t ( t1 ) values (replicate('abc',3000))
    -------------------------
    http://chinadba.cn
    深圳骄子数据库服务网
    最具实战经验的数据库优化,管理,设计,培训
      

  5.   

    如果不考虑“字符串8000可能出界”,那么楼上的已经给出解决方案了,但要考虑的话恐怕就没有什么好的办法了,需要重新考虑表的设计的问题,比如用大字段如text等