2017-10-15 141 views
0

我需要在post_insert觸發器中創建select語句。如果是的話,那麼可能如何呢? 我想檢查另一個表記錄是否存在,那麼它會更新它,否則插入爲新記錄。請幫忙。表格6i如果存在記錄然後更新

我的塊代碼是我要運行

DECLARE 
EXSIST_TYPE varchar2(50); 
EXSIST_NAME varchar2(50); 
EXSIST_COMPANY VARCHAR2(100); 
BEGIN 
SELECT PRO_TYPE, PRO_NAME, COMPANY_NAME INTO EXSIST_TYPE, EXSIST_NAME ,EXSIST_COMPANY FROM STOCK; 
IF 
:PURCHASE_DETAIL.PRO_TYPE <> EXSIST_TYPE AND 
:PURCHASE_DETAIL.PRO_NAME <> EXSIST_NAME AND 
:PURCHASE_DETAIL.COMPANY_NAME <> EXSIST_COMPANY THEN*/ 
IF 
:PURCHASE.RADIO_TYPE = 'PURCHASE' THEN 
INSERT INTO STOCK(
PRO_TYPE  , 
PRO_NAME  , 
COMPANY_NAME , 
QUANTITY  , 
PURCHASE_RATE, 
SALE_RATE , 
RACK_NUM  
) 
VALUES 
(
:PURCHASE_DETAIL.PRO_TYPE, 
:PURCHASE_DETAIL.PRO_NAME, 
:PURCHASE_DETAIL.COMPANY_NAME, 
:PURCHASE_DETAIL.QUANTITY, 
:PURCHASE_DETAIL.PRICE, 
:PURCHASE_DETAIL.SALE_PRICE, 
:PURCHASE_DETAIL.RACK_NUM 
); 
END IF; 
ELSIF 
:PURCHASE_DETAIL.PRO_TYPE = EXSIST_TYPE AND 
:PURCHASE_DETAIL.PRO_NAME = EXSIST_NAME AND 
:PURCHASE_DETAIL.COMPANY_NAME = EXSIST_NAME THEN 
IF 
:PURCHASE.RADIO_TYPE = 'PURCHASE' THEN 
UPDATE STOCK SET 
STOCK.QUANTITY = STOCK.QUANTITY+:PURCHASE_DETAIL.QUANTITY 
WHERE 
STOCK.PRO_TYPE = :PURCHASE_DETAIL.PRO_TYPE AND 
STOCK.PRO_NAME = :PURCHASE_DETAIL.PRO_NAME AND 
STOCK.COMPANY_NAME= :PURCHASE_DETAIL.COMPANY_NAME; 
ELSIF 
:PURCHASE.RADIO_TYPE = 'PRCH_RETURN' THEN 
UPDATE STOCK SET 
STOCK.QUANTITY = STOCK.QUANTITY-:PURCHASE_DETAIL.QUANTITY 
WHERE 
STOCK.PRO_TYPE = :PURCHASE_DETAIL.PRO_TYPE AND 
STOCK.PRO_NAME = :PURCHASE_DETAIL.PRO_NAME AND 
STOCK.COMPANY_NAME = :PURCHASE_DETAIL.COMPANY_NAME; 
END IF; 
END IF; 
END; 
+0

請幫助這個話題 –

回答

0

你從來沒有說過,當你運行該代碼發生了什麼事。

總之:你提到的要求(「如果那麼它存在,它會更新它,否則插入」)看起來像一個優秀候選人MERGE語句(也稱爲UPSERT,如更新和插入的組合)。

由於Forms 6i是一款老式的軟件,我非常確定MERGE不能直接在那裏使用。但是,如果基礎數據庫至少爲9i,則MERGE將工作 - 創建一個包含MERGE的存儲過程,並將表單項的值作爲參數傳遞。

下面是一個例子(從here服用;看看更多的例子,我懶得去創造我自己的代碼。):

MERGE INTO employees e 
    USING hr_records h 
    ON (e.id = h.emp_id) 
    WHEN MATCHED THEN 
    UPDATE SET e.address = h.address 
    WHEN NOT MATCHED THEN 
    INSERT (id, address) 
    VALUES (h.emp_id, h.address); 
相關問題