PL / SQL编译错误,可以找出什么是坏的

时间:2015-01-20 11:17:44

标签: sql oracle plsql oracle-sqldeveloper

所以我的SQL程序如下:

create or replace procedure generate_sample_dd_contracts(cnt in pls_integer)
is
begin
  for cur in (select * from (select * from tc_client cli where not exists (select 1 from tc_direct_debit dd where dd.cli_id=cli.id) order by dbms_random.value) where rownum < dbms_random.value(2,100))
    -- select query works fine and gets clients as needed
    loop
      for i in 0..cnt
        loop
          insert into tc_direct_debit (cli_id, ref_number) values (cur.id, tc_ref_num_seq.nextval);
          -- this insert also works separately from procedure, I have autoinc trigger
        end loop;
      end loop;
  commit;
end;
/
begin
  generate_sample_dd_contracts(3);
end;

首先选择循环选择查询结果 enter image description here

同样,当我运行该程序时,tc_direct_debit ID被更新(进入子句),但表中没有数据。

错误报告:

PROCEDURE GENERATE_SAMPLE_DD_CONTRACTS compiled
Error starting at line 19 in command:
begin
  generate_sample_dd_contracts(3);
end;
Error report:
ORA-00001: unique constraint (T.U_DIRECT_DEBIT_CLI_ID) violated
ORA-06512: at "T.GENERATE_SAMPLE_DD_CONTRACTS", line 9
ORA-06512: at line 2
00001. 00000 -  "unique constraint (%s.%s) violated"
*Cause:    An UPDATE or INSERT statement attempted to insert a duplicate key.
           For Trusted Oracle configured in DBMS MAC mode, you may see
           this message if a duplicate entry exists at a different level.
*Action:   Either remove the unique restriction or do not insert the key.

据此我了解插入尝试添加已存在但仍为tc_direct_debit的cli_id(已将其截断多次以确定)。

tc_direct_debit表

`CREATE TABLE TC_DIRECT_DEBIT
(ID NUMBER NOT NULL
, CLI_ID NUMBER NOT NULL 
, REF_NUMBER VARCHAR2(20) NOT NULL 
, CONSTRAINT TC_DIRECT_DEBIT_PK PRIMARY KEY (ID)
, CONSTRAINT U_DIRECT_DEBIT_CLI_ID UNIQUE (CLI_ID)
, CONSTRAINT TC_DIRECT_DEBIT_CLI_FK FOREIGN KEY (CLI_ID) REFERENCES TC_CLIENT (ID)
);`

2 个答案:

答案 0 :(得分:2)

对于光标中的每一行,您可以插入四次(从0到3)。

执行我认为你想做的事情的代码是:

create or replace procedure generate_sample_dd_contracts(cnt in pls_integer)
is
declare 
i number;
begin
i:=0;
  for cur in (select * from (select * from tc_client cli where not exists (select 1 from tc_direct_debit dd where dd.cli_id=cli.id) order by dbms_random.value) where rownum < dbms_random.value(2,100))
    loop
      i:=i+1;
      exit when i > cnt;
      insert into tc_direct_debit (cli_id, ref_number) values (cur.id, tc_ref_num_seq.nextval);
    end loop;
  commit;
end;
/

或者更好,只需更改光标代码:

create or replace procedure generate_sample_dd_contracts(cnt in pls_integer)
is
begin
  for cur in (select * from (select * from tc_client cli where not exists (select 1 from tc_direct_debit dd where dd.cli_id=cli.id) order by dbms_random.value) where rownum <= cnt)
    loop
      insert into tc_direct_debit (cli_id, ref_number) values (cur.id, tc_ref_num_seq.nextval);
    end loop;
  commit;
end;
/

答案 1 :(得分:1)

似乎CLI_ID被称为unique key,这意味着不会不惜任何代价接受该假设。过程line 9中的wherears已经循环插入

 for i in 0..cnt
   loop
     insert into tc_direct_debit (cli_id, ref_number) values (cur.id, tc_ref_num_seq.nextval);
   -- this insert also works separately from procedure, I have autoinc trigger
 end loop;

删除唯一约束U_DIRECT_DEBIT_CLI_ID将解决您的问题