2016-12-01 273 views
-1

我正在使用Oracle SQL Developer在Oracle12c中編寫過程。我需要做的是檢查一張表以查看是否存在記錄。如果確實如此,那麼我需要對其進行編輯。如果沒有,那麼我需要添加它。Oracle IF/ELSE語句

所以,我的 「僞」 邏輯將是這個樣子:

If (Select * from MyTable where MyValue = 0) has more than one record, then 
    This will be an EDIT line of code 
else 
    This will be an ADD line of code 
end 

我怎麼會寫在Oracle SQL Developer中?

編輯:

目前我的代碼看起來是這樣的,我需要添加一塊要更新的記錄。如果Line_ID存在,我需要更新記錄。如果不是,我需要添加它。

create or replace PROCEDURE CST_WRITE_FEEDBACK (
v_AuditID IN NUMBER, 
v_BenefitID IN NUMBER, 
v_Network1 IN VarChar, 
v_Network2 IN VarChar, 
v_Network3 IN VarChar, 
v_Network4 IN VarChar, 
v_Issue1 IN NUMBER, 
v_Issue2 IN NUMBER, 
v_Issue3 IN NUMBER, 
v_Issue4 IN NUMBER, 
v_Resolution1 IN NUMBER, 
v_Resolution2 IN NUMBER, 
v_Resolution3 IN NUMBER, 
v_Resolution4 IN NUMBER, 
v_Status IN NUMBER, 
v_Comment IN VarChar, 
v_LineID IN NUMBER, 
v_EmpID IN NUMBER 
) 
AS 
BEGIN 

IF 

    INSERT INTO F_AUDIT_LINE ("AUDIT_ID", "BENEFIT_ID", "NETWORK", "NETWORK2", "NETWORK3", "NETWORK4", 
    "ISSUE_ID", "ISSUE_ID2", "ISSUE_ID3", "ISSUE_ID4", 
    "RESOLUTION_ID", "RESOLUTION_ID2", "RESOLUTION_ID3", "RESOLUTION_ID4", 
    "LINE_STATUS") 
    VALUES (v_AuditID, v_BenefitID, v_Network1, v_Network2, v_Network3, v_Network4, 
    v_Issue1, v_Issue2, v_Issue3, v_Issue4, 
    v_Resolution1, v_Resolution2, v_Resolution3, v_Resolution4, 
    v_Status 
); 

END CST_WRITE_FEEDBACK; 
+2

只需使用Oracle上的合併功能 –

+0

您可以在此擴展嗎? –

+0

我已經刪除了SQL Developer的標籤,因爲代碼不依賴於您使用的編輯器。 – Kacper

回答

5
declare 
    i number; 
begin 
    Select count(*) into i from MyTable where MyValue = 0; 
If (i > 0) then 
    update ... 
else 
    insert ... 
end; 
/

但是在留言中提到你可以擁有它:

merge into MyTable m 
using (select v_AuditID, v_BenefitID, v_Network1, v_Network2, _Network3, _Network4, 
       v_Issue1, v_Issue2, v_Issue3, v_Issue4, 
       v_Resolution1, v_Resolution2, v_Resolution3, v_Resolution4, v_Status 
     from dual) d 
on (d.MyValue = m.Myvalue) 
when matched then 
    update set .. 
when not matched then 
    insert ... 

如果您提供的程序代碼,我們可以爲您擴展命令。

+0

請參閱我的更新瞭解我當前的代碼。 –

+5

@JohnnyBones這不是一個代碼編寫服務給你。除了Kacper的回答外,還有大量關於在互聯網上合併的有用信息,例如[here](https://oracle-base.com/articles/9i/merge-statement)。爲什麼不把自己的合併聲明寫下去,然後在被卡住時尋求幫助? – Boneist

+0

@Boneist - 寫這個之前你看過我的個人資料嗎? –