次のテーブルがあります
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;