-1
我有表如何創建一個刪除存儲過程,以確保它在另一個表中有關聯記錄時無法刪除?
- 供應商(supplier_id(PK),姓名,地址)supplier_invoice(supp_invoice-ID(PK),平衡)
- supplier_product(supp_prod_id(PK),Supplier_id(FK),PRODUCT_ID( FK),supp_invoice_id(FK))
我試圖執行刪除過程:
set serveroutput on;
create or replace
procedure delete_supp
(d_supplier_id int)
is
v_count int;
begin
select count(*) into v_count from supplier_product where d_supplier_id=supplier_id;
if v_count > 0 then
dbms_output.put_line('Supplier cannot be deleted because there is an existing invoice in the system');
else
begin
delete from supplier where supplier_id=d_supplier_id;
DBMS_OUTPUT.PUT_LINE('Deleted ' || SQL%ROWCOUNT || ' Rows.');
commit;
end;
end if;
Exception
when others then
dbms_output.put_line('Delete failed');
end;
/
這個過程的作品,但不是檢查,如果該記錄存在於supplier_product中,我想確保供應商在supplier_invoice表中打開發票時不能刪除。我嘗試在supplier_invoice表中循環,但無法使其工作。
這不是問題的答案。但是,您不應刪除任何主記錄,而是將相關數據(供應商發票)移至歸檔表。 – shahkalpesh 2012-03-27 06:06:54
供應商發票的唯一相關表是supplier_product表。但我把這張桌子視爲購買表格,基本上所有的購買都被記錄下來。購買表的唯一好處是隻要進行購買,它就會更新庫存表並維護產品的總數。但是我有Supplier_invoice,將根據付款進行處理。因此,我試圖執行一個proc,當且僅當找到現在相關的發票時,才允許刪除供應商。 – pragya 2012-03-27 06:19:16
'SELECT 1 from supplier s,supplier_product sp,supplier_invoice si where s.supplier_id = sp.supplier_id and sp.supp_invoice_id = si.supp_invoice_id;'如果此查詢返回任何值,則不刪除,如果不是則刪除... .i認爲這將解決您的問題 – 2012-03-27 06:26:51