2012-11-21 72 views
1

我有接收三個字符串參數分割多列分號分隔的字符串和創建記錄

1. p_ReqIDs VARCHAR2 
2. p_ItemIDs VARCHAR2 
3. p_Qtys VARCHAR2 

上述3個變量包含分號分隔值這樣的存儲過程:

p_ReqIDs := "56;56;56;" 
p_ItemIDs := "3;2;1;" 
p_Qtys := "400;300;200;" 

我需要拆分值並創建像這樣的行:

p_ReqIDs p_ItemIDs p_Qtys 
------------------------------------ 
    56   3  400 
    56   2  300 
    56   1  200 

我需要q uery分割並插入到表中。

感謝

回答

2

您可以處理參數如下:

SQL> declare 
    2 type l_rec_type is record(
    3  r_ReqIDs varchar2(31), 
    4  r_ItemIDs varchar2(31), 
    5  r_Qtys varchar2(31) 
    6 ); 
    7 
    8 type l_rec_list is table of l_rec_type; 
    9 -- your parameters. 
10 l_ReqIDs constant varchar2(31) := '56;56;56;'; 
11 l_ItemIDs constant varchar2(31) := '3;2;1;'; 
12 l_Qtys constant varchar2(31) := '400;300;200;'; 
13 
14 l_rec l_rec_list; 
15 begin 
16 
17 with Parameters(param) as(
18  select l_ReqIDs from dual union all 
19  select l_ItemIDs from dual union all 
20  select l_Qtys from dual 
21 ), 
22 Occurrences(oc) as(
23  select level 
24  from (select max(regexp_count(param, '[^;]+')) moc 
25     from parameters) s 
26  connect by level <= s.moc 
27 ) 
28 select max(res1) 
29  , max(res2) 
30  , max(res3) 
31 bulk collect into l_rec 
32 from (select decode(param, l_ReqIDs, res) res1 
33    , decode(param, l_ItemIDs,res) res2 
34    , decode(param, l_Qtys, res) res3 
35    , rn 
36   from (select param, regexp_substr(param, '[^;]+', 1, o.oc) res 
37      , row_number() over(partition by param order by param) rn 
38      from parameters p 
39     cross join occurrences o 
40     ) 
41   ) 
42 group by rn; 
43 
44 for i in l_rec.first..l_rec.last 
45 loop 
46  dbms_output.put_line(l_rec(i).r_ReqIDs || ' ' || l_rec(i).r_ItemIDs || ' ' || l_rec(i).r_Qtys); 
47 end loop; 
48 end; 
49/



56 2 200 
56 1 300 
56 3 400 

PL/SQL procedure successfully completed 

如果您只是需要處理的數據插入表中的唯一insert into聲明和查詢,將需要(bulk collect into l_rec必須拆除):

insert into your_table(<<columns>>) 
    with Parameters(param) as(
    select l_ReqIDs from dual union all 
    select l_ItemIDs from dual union all 
    select l_Qtys from dual 
    .... 
    -- the rest of the query from the above pl/sql block. 

您還可以將整個記錄插入表中(在需要對提取進行額外處理的情況下插入前ED元素)如下:

  • 如果被插入

    for i in l_rec.first..l_rec.last 
    loop 
        insert into your_table 
        values l_rec(i); 
    end loop; 
    
  • 如果列的表中的數字是大於的數目等於元件的數目在表的列數插入

    for i in l_rec.first..l_rec.last 
    loop 
        insert into (select column1, .. ,columnn from your_table) 
        values l_rec(i); 
    end loop; 
    
2

我不喜歡這樣:)簡短而親切的值。

create or replace procedure proc1(p_ReqID varchar2,p_ItemID varchar2,p_ItemQty varchar2) 
is 
begin 
insert into test(req_id,item_id,itemqty) 
select regexp_substr(req_id,'[^;]+',1,level), 
     regexp_substr(item_id,'[^;]+',1,level), 
     regexp_substr(item_qty,'[^;]+',1,level) 
    from (
     select p_ReqID Req_ID,p_ItemID item_id,p_ItemQty item_qty 
     from dual 
    ) 
    connect by regexp_substr(req_id,'[^;]+',1,level) is not null; 
end; 
+0

是的,更短。如果第二個或第三個等參數具有更多元素,則只需在「連接by」子句中添加「OR」條件即可。否則,你可能會失去有價值的信息:) –

+0

尼古拉斯在我的情況下,它永遠不會發生,因爲我拉相同的REQ_ID的詳細記錄,並傳遞給.NET的Web服務的甲骨文。所以所有的列都會有相同的值總是:) – user342944

相關問題