PL / SQL批量集合插入语法

时间:2013-07-25 18:49:04

标签: plsql insert bulk-collect

我的任务是将一个新的transaction_code插入到一个包含同一个表中的min_cost和max_cost的表中。我想要做的是从事件代码为2的县获取min_cost,使用事务代码为4的县获取max_cost。这是我到目前为止。

我在我收到它们的行旁边写了我正在收到的错误。我也尝试使用数组作为表,我不认为它会工作,但我把它们留在那里,所以你会理解我尝试的东西(它们被注释掉了)。

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

非常感谢任何正确方向的推动,谢谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

并查看是否可以改善目标。

分享并享受。