SQL Query to concatenate column values from multiple rows in Oracle

前端 未结 10 1230
鱼传尺愫
鱼传尺愫 2020-11-21 07:12

Would it be possible to construct SQL to concatenate column values from multiple rows?

The following is an example:

Table A

PID
A
B
C

10条回答
  •  被撕碎了的回忆
    2020-11-21 07:21

    In the select where you want your concatenation, call a SQL function.

    For example:

    select PID, dbo.MyConcat(PID)
       from TableA;
    

    Then for the SQL function:

    Function MyConcat(@PID varchar(10))
    returns varchar(1000)
    as
    begin
    
    declare @x varchar(1000);
    
    select @x = isnull(@x +',', @x, @x +',') + Desc
      from TableB
        where PID = @PID;
    
    return @x;
    
    end
    

    The Function Header syntax might be wrong, but the principle does work.

提交回复
热议问题