2016-08-04 84 views
0

是否可以這樣做,method_list和currency_list的值將來自表單。對於每種方法,每種方法都需要插入插入次數,如果存在3種貨幣,那麼對於VISA方法,它必須插入一行3次,然後對於MASTER方法插入3次。存在每種方法的配置。將「變量」存儲在列表中,然後將它們插入到數據庫中foreach其他列表column_values

declare 
    method_list sys.odcivarchar2list; 
    currency_list sys.odcivarchar2list; 
begin 
    method_list := sys.odcivarchar2list( 
    (method:='VISA' , deposit:=1), 
    (method:='MASTER' , deposit:=0) 
); 
    currency_list := sys.odcivarchar2list('EUR','USD','GBP'); 
    INSERT INTO table (method, currency, deposit, confcode) 
    SELECT m.method, c.column_value, m.deposot, conf.code 
    FROM method_list m, currency_list c, conf 
    WHERE conf.method = m.method 
end; 

回答

1

你當然可以從陣列通過將table()他們周圍的from子句中選擇:

1 declare 
    2 method_list sys.odcivarchar2list; 
    3 currency_list sys.odcivarchar2list; 
    4 begin 
    5 method_list := sys.odcivarchar2list(
    6  'VISA', 
    7  'MASTER' 
    8 ); 
    9 currency_list := sys.odcivarchar2list('EUR','USD','GBP'); 
10 for r in (
11 SELECT m.column_value m_value, c.column_value c_value 
12 FROM table(method_list) m, table(currency_list) c 
13 ) 
14 loop 
15  dbms_output.put_line (r.m_value || ' ' || r.c_value); 
16 end loop; 
17* end; 
SQL>/
VISA EUR 
VISA USD 
VISA GBP 
MASTER EUR 
MASTER USD 
MASTER GBP 

PL/SQL procedure successfully completed. 

但是,你不能用那種你method_list做僞JSON語法的(儘管在12c真正的JSON is supported in PL/SQL)。

但是你可以創建自己的類型,如:

declare 
    method_list method_tab_t; 
    currency_list sys.odcivarchar2list; 
begin 
    method_list := method_tab_t(
    method_obj_t('VISA',1), 
    method_obj_t('MASTER',0) 
); 
    currency_list := sys.odcivarchar2list('EUR','USD','GBP'); 
    for r in (
    SELECT m.method, m.deposit, c.column_value c_value 
    FROM table(method_list) m, table(currency_list) c 
) 
    loop 
    dbms_output.put_line (r.method || ' ' || r.deposit || ' ' || r.c_value); 
    end loop; 
end; 

create type method_obj_t is object (method varchar2(10), deposit integer); 

create type method_tab_t is table of method_obj_t; 

然後,你可以從那些使用table()選擇

相關問題