我的程序问题如下。我有多次购买的不同商品编号(订单编号)的记录。当我根据项目编号返回部分购买时,它应该从items
表中删除记录并将项目详细信息插入名为`deleted_items'的表中。问题是当我删除部分订单时表一次,表deleted_Items将出现此错误:
ORA-06512:第4行 00001. 00000 - “违反了唯一约束(%s。%s)” *原因:UPDATE或INSERT语句尝试插入重复键。 对于在DBMS MAC模式下配置的Trusted Oracle,您可能会看到 如果重复条目存在于不同级别,则显示此消息。 *操作:删除唯一限制或不插入密钥。
create or replace PROCEDURE RET_Item(Item_no number(5))
AS
CURSOR Return_i
IS
SELECT * FROM Items
WHERE item_num = Item_no
AND Item_num IN (SELECT Item_num FROM deleted_items);
cur1 Return_i%ROWTYPE;
BEGIN
OPEN Return_i;
LOOP
FETCH Return_i INTO cur1;
EXIT WHEN Return_i%NOTFOUND;
IF cur1.Delivered IS NOT NULL THEN
Insert into deleted_itmes values(
cur1.Order_No,cur1.Item_num,cur1.Delivered,User,sysdate,
'Item_returned');
DELETE FROM Items WHERE Item_num = Item_no ;
DELETE FROM Purchase where Item_num = Item_no;
DBMS_OUTPUT.PUT_LINE('Item number ' || Item_no ||' deleted! ');
ELSIF cur1.Delivered IS NULL THEN
DBMS_OUTPUT.PUT_LINE('Cannot return item: '|| cur1.Item_num);
END IF;
END LOOP;
CLOSE Return_i;
END RET_item;
项目表
Order_No Item_num Delivered
---------- ---------- ---------
1001 1234 12-JUN-15
1002 5678 01-JAN-15
1003 9087 05-FEB-15
购买表
Order_No Item_num Quantity
---------- ---------- ---------------
1001 1234 10
1001 9087 7
1002 9087 10
1003 1234 1
1003 5678 12
Deleted_Item表
项目表
Order_No Item_num Delivered Name_on-order Date_of_Ret Status
答案 0 :(得分:0)
从您的代码中不清楚,您要插入哪个表..您可以粘贴整个INSERT语句吗?
如果要插入DELETED_ITEMS表,那么为什么需要
AND Item_num IN (SELECT Item_num FROM deleted_items)
不应该
AND Item_num NOT IN (SELECT Item_num FROM deleted_items)