/* 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)