求助:Oracle PL/SQL: ORA-00001: 违反必须为唯一的限制条件 我查出的数据没有重复的,为什麽报错?
关注:77 答案:2 手机版
解决时间 2021-02-24 09:15
提问者莪早已看透了誓言
2021-02-23 14:58
我的存储过程为:
--插入查询出组名称到表SGrouPNeedCarCount
set serveroutput on
create or replace procedure pr_NeedCarGroupName Authid Current_User is
type sp_NeedCarGroupName_Cursor is ref cursor;
NeedCarGroupName_Cursor sp_NeedCarGroupName_Cursor;
v_groupName SGroupStorageLocation.d0%type;
begin
open NeedCarGroupName_Cursor for select distinct d0 from SGroupStorageLocation order by d0;
loop
fetch NeedCarGroupName_Cursor into v_groupName;
insert into SGrouPNeedCarCount (d0) values (v_groupName);
exit when NeedCarGroupName_Cursor%notfound;
dbms_output.put_line('组名称为:'||v_groupName);
end loop;
close NeedCarGroupName_Cursor;
end;
查出来的语句为:
Procedure created
SQL> exec pr_NeedCarGroupName;
组名称为:MODULE(18.5LED)
组名称为:MODULE(23.6LED)
组名称为:MODULE(B131)
组名称为:MODULE(B137)
组名称为:MODULE(B64机种(长))
组名称为:MODULE(B98机种)
组名称为:MODULE(MI5B1)
组名称为:MODULE(MJ0A1)
组名称为:MODULE(ML5H1)
组名称为:MODULE(ML6H1)
组名称为:MODULE(MM0Z1)
组名称为:MODULE(四类LED机种)
组名称为:MODULE(非NB机种)
组名称为:NB LED BTAG(APPLE+FUJI)
组名称为:NB LED BTAG(B26)
组名称为:NB LED BTAG(EDP)
组名称为:NB LED 普通(ALL)
组名称为:NB LED 普通(HP机种)
组名称为:NB LED 普通(除APPLE、FUJI、HP、EDP机种)
组名称为:灯管Inverter(四灯管)
组名称为:灯管Inverter(双灯管)
报错误为:
begin pr_NeedCarGroupName; end;
ORA-00001: 违反必须为唯一的限制条件 (NBTESTDATA.SGROUPNEEDCARCOUNTPK)
ORA-06512: 在 "NBTESTDATA.PR_NEEDCARGROUPNAME", line 9
ORA-06512: 在 line 2
最佳答案
二级知识专家妳説啲訫動
2021-02-23 16:33
exit when NeedCarGroupName_Cursor%notfound;
insert into SGrouPNeedCarCount (d0) values (v_groupName);
把这两句换下位置。
另外,为什么要用游标循环插入呢,一句SQL就可以解决了啊。
全部回答
1楼招人烦°惹人厌
2021-02-23 17:24
修改一下循环语句,按下列形式把insert语句和exit语句调换顺序,先判断结束,再插入记录。
loop
fetch needcargroupname_cursor into v_groupname;
exit when needcargroupname_cursor%notfound;
insert into sgroupneedcarcount (d0) values (v_groupname);
dbms_output.put_line('组名称为:'||v_groupname);
end loop;
我要举报
如以上信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
推荐资讯
大家都在看