日期:2014-05-16 浏览次数:20399 次
建表语句: create table SP_ALERT_PA_GROUP ( ID INTEGER not null, ALERTCODE VARCHAR2(8), PAINNERCODE VARCHAR2(20) ) insert into sp_alert_pa_group (ID, ALERTCODE, PAINNERCODE) values (10081, 'AL000027', 'R0000005'); insert into sp_alert_pa_group (ID, ALERTCODE, PAINNERCODE) values (10082, 'AL000027', 'R0000006'); insert into sp_alert_pa_group (ID, ALERTCODE, PAINNERCODE) values (10083, 'AL000026', 'R0000001'); insert into sp_alert_pa_group (ID, ALERTCODE, PAINNERCODE) values (10084, 'AL000026', 'R0000002');
CREATE OR REPLACE FUNCTION getpa(alertcode VARCHAR2) RETURN VARCHAR2 IS pastr VARCHAR2(4000); BEGIN FOR cur IN (SELECT painnercode FROM sp_alert_pa_group WHERE alertcode=alertcode) LOOP pastr := pastr||cur.painnercode||','; END LOOP; RETURN pastr; END; 然后使用下面的SQL查询即可! SELECT DISTINCT alertcode ,getpa(alertcode) FROM sp_alert_pa_group f
SELECT wmsys.wm_concat(painnercode) FROM sp_alert_pa_group
select substr(max(sys_connect_by_path(painnercode,',')),2) paramcode from (select a.*,row_number()over(order by painnercode) rn from sp_alert_pa_group a ) start with rn=1 connect by rn-1=prior rn
CREATE FUNCTION getpa(v_alertcode VARCHAR(8)) RETURNS VARCHAR(4000) LANGUAGE SQL SPECIFIC getpa --SPECIFIC getpa 指定函数名称 gp: BEGIN ATOMIC DECLARE pastr VARCHAR(4000) DEFAULT ''; FOR cur AS SELECT painnercode FROM sp_alert_pa_group WHERE alertcode=v_alertcode DO SET pastr=pastr || cur.painnercode || ','; END FOR; RETURN pastr; END gp;