我有以下表格
CREATE TABLE A2SKU
(
SKUID NUMBER NOT NULL,
SKUNAME VARCHAR2(20),
SKUQTY NUMBER,
CONSTRAINT A2SKU_PK PRIMARY KEY (SKUID)
);
CREATE TABLE A2LOC
(
LOCID NUMBER NOT NULL,
LOCNAME VARCHAR2(20),
LOCQTY NUMBER,
MAXQTY NUMBER,
CONSTRAINT A2LOC_PK PRIMARY KEY (LOCID)
);
Create Table A2BOX (
boxId NUMBER Primary Key,
skuId NUMBER,
locId NUMBER,
constraint FK_A2BOX_SKUID foreign key (skuid) references A2SKU,
constraint FK_A2BOX_LOCID foreign key (locid) references A2LOC
);
CREATE SEQUENCE A2BOXSEQ;
我想从 A2Box 表中删除所有具有匹配 SKU 的框,并将每个框的 A2Loc 表中匹配位置的 LocQty 调整 1
我如何使用存储过程来做到这一点。
我尝试了自己并想出了这个
create or replace procedure A2DeleteSKU
(
pskuid IN number
)
AS
CURSOR row_cursor IS
SELECT boxid,locid from A2BOX where skuid = pskuid;
skuid_count Number;
skuid_check exception;
sku_rec row_cursor%rowtype;
Begin
select count(*) into skuid_count from A2SKU where skuid = pskuid;
if skuid_count <1 then
raise skuid_check;
End If;
FOR sku_rec in row_cursor
LOOP
delete from a2box where boxid = sku_rec.boxid;
update A2LOC set locqty = locqty -1 where locid = sku_rec.locid;
END LOOP;
delete from A2SKU where skuid =pskuid;
commit;
Exception
When skuid_check then
RAISE_APPLICATION_ERROR(-20014, 'Delete Box failed. Boxid does not exist: '||TO_CHAR(pskuid));
When others then
raise;
End A2DeleteSku;