oracle 利用函數實現多行一列資料合併效果

來源:互聯網
上載者:User

標籤:多行一列資料合併

/* Formatted on 2013-1-11 13:23:55 (QP5 v5.185.11230.41888) */
 CREATE OR REPLACE FUNCTION GetGiftsName (rid  in number)//rid 穿的參數
    RETURN VARCHAR2
  AS
     STR   VARCHAR2 (1000);                                           /*返回的資料集*/
  BEGIN
     DECLARE
        v_medCode VARCHAR2 (100);                                 /*存放臨時變數的地方*/
 
       CURSOR D_cursor
       IS
         select b.cproname from cc_promotions a left join cc_progifts b on a.ipromosid=b.ipromosid where a.ipromosid=rid and b.bgift=‘‘;
    BEGIN
       OPEN D_cursor;
     loop
     fetch D_cursor into v_medCode;
     EXIT WHEN D_cursor%NOTFOUND;
    if length(STR) > 0 THEN
        STR := STR ||‘,‘|| v_medCode;
     ELSE
        STR := STR || v_medCode;
     END IF;
     END LOOP;
     close D_cursor;
    end;
     RETURN STR;
 END GetGiftsName;


select GetGiftsName(428) from dual


實現的效果為可以把相同id下的一個欄位資料查詢出來並且以逗號分開( 加州西梅,加州西梅2)



oracle 利用函數實現多行一列資料合併效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.