Question : update table if record does not exist otherwise insert record

I have a programmer who is trying to get this code to work.
I don't think the logic is correct - can you tell me what he has wrong?

He wants to check the table to see if a record exists - if it exists he wants to update it - if it does not exist he wants to insert the record.
##########################################################
CREATE OR REPLACE PROCEDURE CNSADMIN.UpdateApprovedScoutRule
(
    p_SID IN ApprovedScoutRule.SID%TYPE,
    p_RuleText IN ApprovedScoutRule.RuleText%TYPE,
    p_DefaultPriority IN ApprovedScoutRule.DefaultPriority%TYPE,
    p_IsSoRule IN ApprovedScoutRule.IsSoRule%TYPE,
    p_IsIgnored IN ApprovedScoutRule.IsIgnored%TYPE
)
AS
BEGIN

    MERGE INTO ApprovedScoutRule asr
    USING (SELECT SID,
             RuleText,
             DefaultPriority,
             IsSoRule,
             IsIgnored FROM ApprovedScoutRule) asr2  
    ON (asr2.SID = asr.SID)
    WHEN MATCHED THEN
       --UPDATE EXISTING RULE
        UPDATE
            SET
                 asr.RuleText = p_RuleText,
                 asr.DefaultPriority = p_DefaultPriority,
                 asr.IsSoRule = p_IsSoRule,
                 asr.IsIgnored = p_IsIgnored
            WHERE asr.SID = p_SID
      WHEN NOT MATCHED THEN    
            --INSERT NEW RULE
            INSERT
                (asr.SID,
                 asr.RuleText,
                 asr.DefaultPriority,
                 asr.IsSoRule,
                 asr.IsIgnored)
            VALUES
                (p_SID,
                 p_RuleText,
                 p_DefaultPriority,
                 p_IsSoRule,
                 p_IsIgnored
                );
   
    COMMIT;
   
   
EXCEPTION
        WHEN OTHERS THEN
             RAISE_APPLICATION_ERROR (-20001,
              p_SID    || ':$:' ||
              p_RuleText    || ':$:' ||
              p_DefaultPriority    || ':$:' ||
              p_IsSoRule    || ':$:' ||
              SQLERRM, TRUE) ;

END UpdateApprovedScoutRule;
/
###########################################################

Thanks

Answer : update table if record does not exist otherwise insert record

Check out:
http://jasonvogel.blogspot.com/2008/01/upsert-merge-within-same-table-oracle.html


That lead me to the following test case.  Note: I changed the table name.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
drop table myApprovedScoutRule  purge;
create table myApprovedScoutRule(sid char(1), ruletext char(1), defaultpriority char(1), issorule char(1),isignored char(1));
insert into myApprovedScoutRule values('a','b','c','d','e');
insert into myApprovedScoutRule values('z','z','z','z','z');
commit;

CREATE OR REPLACE PROCEDURE UpdateApprovedScoutRule
(
    p_SID IN myApprovedScoutRule.SID%TYPE,
    p_RuleText IN myApprovedScoutRule.RuleText%TYPE,
    p_DefaultPriority IN myApprovedScoutRule.DefaultPriority%TYPE,
    p_IsSoRule IN myApprovedScoutRule.IsSoRule%TYPE,
    p_IsIgnored IN myApprovedScoutRule.IsIgnored%TYPE
)
AS
BEGIN

    MERGE INTO myApprovedScoutRule asr
    USING (SELECT p_sid sid,
             p_ruletext RuleText,
             p_DefaultPriority DefaultPriority,
             p_IsSoRule IsSoRule,
             p_IsIgnored IsIgnored FROM dual) asr2   
    ON (asr2.SID = asr.SID)
    WHEN MATCHED THEN
       --UPDATE EXISTING RULE
        UPDATE 
            SET
                 asr.RuleText = p_RuleText,
                 asr.DefaultPriority = p_DefaultPriority,
                 asr.IsSoRule = p_IsSoRule,
                 asr.IsIgnored = p_IsIgnored
            WHERE asr.SID = p_SID
      WHEN NOT MATCHED THEN    
            --INSERT NEW RULE
            INSERT
                (asr.SID,
                 asr.RuleText,
                 asr.DefaultPriority,
                 asr.IsSoRule,
                 asr.IsIgnored)
            VALUES
                (p_SID,
                 p_RuleText,
                 p_DefaultPriority,
                 p_IsSoRule,
                 p_IsIgnored
                );
    
    COMMIT;
    
    
EXCEPTION
        WHEN OTHERS THEN 
             RAISE_APPLICATION_ERROR (-20001, 
              p_SID    || ':$:' || 
              p_RuleText    || ':$:' ||
              p_DefaultPriority    || ':$:' ||
              p_IsSoRule    || ':$:' ||
              SQLERRM, TRUE) ;

END UpdateApprovedScoutRule;
/

show errors


exec updateapprovedscoutrule('a','a','a','a','a');
exec updateapprovedscoutrule('b','b','b','b','b');

select * from myApprovedScoutRule;
Random Solutions  
 
programming4us programming4us