2013-07-25 67 views
0

我的任務是將新的transaction_code插入包含來自同一個表的min_cost和max_cost的表中。我想要做的是從事務代碼爲2的縣採取min_cost,並從事務代碼爲4的縣採取max_cost。這是我迄今爲止的情況。PL/SQL批量集合插入語法

我寫了我得到的錯誤,作爲評論旁邊的線我得到他們。我也嘗試使用數組作爲表,我不認爲它會工作,但我把它們留在那裏,所以你會明白我試圖(他們被註釋掉)。

declare 
    type cost_min_array IS TABLE OF court_cost.cost_range_min%type INDEX BY BINARY_INTEGER; 
    type cost_max_array IS TABLE OF court_cost.cost_range_max%type INDEX BY BINARY_INTEGER; 
    v_min_cost cost_min_array; 
    v_min_cost cost_max_array; 
BEGIN 
    begin 
    select cost_range_min -- SQL STATEMENT IGNORED 
    bulk collect into v_min_cost 
    from court_cost 
    where state = 'IL' 
    and transaction_code = 4 
    and end_date is null 
    order by county 
    EXCEPTION WHEN OTHERS THEN --SQL COMMAND NOT PROPERLY ENDED 
    v_min_cost.delete; 
    END; 

    BEGIN 
    select cost_range_max -- SQL STATEMENT IGNORED 
    bulk collect into v_max_cost 
    from court_cost 
    where state = 'IL' 
    and transaction_code = 2 
    and end_date is null 
    order by county 
    EXCEPTION WHEN OTHERS THEN -- SQL COMMAND NOT PROPERLY ENDED 
    v_max_cost.delete; 
    END; 

    for indx in 1..v_min_cost.count 
    loop 
    insert into court_cost -- SQL STATEMENT IGNORED 
     (TRANSACTION_CODE, 
     STATE, 
     COUNTY, 
     COST_RANGE_MIN, 
     BEGIN_DATE, 
     END_DATE, 
     DATE_INSERTED, 
     COURT, 
     COST_RANGE_MAX) 
    select lcc.TRANSACTION_CODE, 
     lcc.STATE, 
     lcc.COUNTY, 
     v_min_cost(indx), 
     lcc.BEGIN_DATE, 
     lcc.END_DATE, 
     lcc.DATE_INSERTED, 
     lcc.COURT, 
     v_max_cost(indx) 
    from court_cost lcc 
--  cost_min_array cmn, 
--  cost_max_array cmx 
    end loop; -- SQL COMMAND NOT PROPERLY ENDED 
end; -- ENCOUNTERED THE SYMBOL ; WHEN EXPECTING LOOP 

任何推向正確的方向將不勝感激,謝謝。

回答

0

您在SQL語句後缺少分號。嘗試

declare 
    type cost_min_array IS TABLE OF court_cost.cost_range_min%type INDEX BY BINARY_INTEGER; 
    type cost_max_array IS TABLE OF court_cost.cost_range_max%type INDEX BY BINARY_INTEGER; 
    v_min_cost cost_min_array; 
    v_min_cost cost_max_array; 
BEGIN 
    begin 
    select cost_range_min -- SQL STATEMENT IGNORED 
    bulk collect into v_min_cost 
    from court_cost 
    where state = 'IL' 
    and transaction_code = 4 
    and end_date is null 
    order by county; -- semi-colon added 
    EXCEPTION WHEN OTHERS THEN --SQL COMMAND NOT PROPERLY ENDED 
    v_min_cost.delete; 
    END; 

    BEGIN 
    select cost_range_max -- SQL STATEMENT IGNORED 
    bulk collect into v_max_cost 
    from court_cost 
    where state = 'IL' 
    and transaction_code = 2 
    and end_date is null 
    order by county; -- semi-colon added 
    EXCEPTION WHEN OTHERS THEN -- SQL COMMAND NOT PROPERLY ENDED 
    v_max_cost.delete; 
    END; 

    for indx in 1..v_min_cost.count 
    loop 
    insert into court_cost -- SQL STATEMENT IGNORED 
     (TRANSACTION_CODE, 
     STATE, 
     COUNTY, 
     COST_RANGE_MIN, 
     BEGIN_DATE, 
     END_DATE, 
     DATE_INSERTED, 
     COURT, 
     COST_RANGE_MAX) 
    select lcc.TRANSACTION_CODE, 
     lcc.STATE, 
     lcc.COUNTY, 
     v_min_cost(indx), 
     lcc.BEGIN_DATE, 
     lcc.END_DATE, 
     lcc.DATE_INSERTED, 
     lcc.COURT, 
     v_max_cost(indx) 
    from court_cost lcc; -- semi-colon added 
--  cost_min_array cmn, 
--  cost_max_array cmx 
    end loop; -- SQL COMMAND NOT PROPERLY ENDED 
end; -- ENCOUNTERED THE SYMBOL ; WHEN EXPECTING LOOP 

看看是否改善了事情。

分享和享受。

+0

對不起,它需要一段時間來回應,這些改變似乎永遠運行。我今天沒有時間研究它,但我會報告回來。感謝您的幫助! – user2405778

+0

看一下代碼,看起來你會得到很多插入到COURT_COST中的行。 'INSERT INTO COURT_COST'有一個不合格的SELECT,所以它將返回COURT_COST中的每一行。在我看來,它看起來像你將得到一個COURT_COST表的完整副本,重新插入到它自己的v_min_cost數組中的每個值。所以我們假設COURT_COST以10000行開始,並且v_min_cost中有500個值。在這種情況下,在我看來,INSERT完成後,您將在表中添加500 * 10000 = 5,000,000行。 –

1

您錯過了導致錯誤的代碼中的分號三次。在代碼中,我已經添加了它們,請檢查代碼現在運行。忽略的語句即將到來,因爲在它的末尾沒有添加分號,甚至在同一語句中考慮了異常部分,這就是爲什麼會出錯的原因。

declare 

    type cost_min_array IS TABLE OF court_cost.cost_range_min%type INDEX BY BINARY_INTEGER; 
    type cost_max_array IS TABLE OF court_cost.cost_range_max%type INDEX BY BINARY_INTEGER; 
    v_min_cost cost_min_array; 
    v_min_cost cost_max_array; 

BEGIN 

    begin 

    select cost_range_min -- SQL STATEMENT IGNORED 
    bulk collect into v_min_cost 
    from court_cost 
    where state = 'IL' 
    and transaction_code = 4 
    and end_date is null 
    order by county--semi colon added 

    EXCEPTION WHEN OTHERS THEN --SQL COMMAND NOT PROPERLY ENDED 
    v_min_cost.delete; 

END; 


BEGIN 

    select cost_range_max -- SQL STATEMENT IGNORED 
    bulk collect into v_max_cost 
    from court_cost 
    where state = 'IL' 
    and transaction_code = 2 
    and end_date is null 
    order by county--semi colon added 
    EXCEPTION WHEN OTHERS THEN -- SQL COMMAND NOT PROPERLY ENDED 
    v_max_cost.delete; 
    END; 

    for indx in 1..v_min_cost.count 

    loop 

    insert into court_cost -- SQL STATEMENT IGNORED 
     (TRANSACTION_CODE, 
     STATE, 
     COUNTY, 
     COST_RANGE_MIN, 
     BEGIN_DATE, 
     END_DATE, 
     DATE_INSERTED, 
     COURT, 
     COST_RANGE_MAX) 
    select lcc.TRANSACTION_CODE, 
     lcc.STATE, 
     lcc.COUNTY, 
     v_min_cost(indx), 
     lcc.BEGIN_DATE, 
     lcc.END_DATE, 
     lcc.DATE_INSERTED, 
     lcc.COURT, 
     v_max_cost(indx) 
    from court_cost lcc;--semi colon added 
--  cost_min_array cmn, 
--  cost_max_array cmx 
    end loop; -- SQL COMMAND NOT PROPERLY ENDED 
end; -- ENCOUNTERED THE SYMBOL ; WHEN EXPECTING LOOP