Oracle Error PLS-00103. How can you check an existing record and update it or insert based on this condition?

I need to check if an entry in the table exists or not from the SELECT statement. If the record exists, upgrade, otherwise create a record in the table. I try, but I get an error PLS-00103.

These are the errors that I get when I run my code in DBVisaulzier:

18:00:09  [DECLARE - 0 row(s), 0.000 secs]  [Error Code: 6550, SQL State: 65000]  ORA-06550: line 2, column 12:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:

:= . ( @ % ; not null range default character
18:00:09  [BEGIN - 0 row(s), 0.000 secs]  [Error Code: 6550, SQL State: 65000]  
ORA-06550: line 2, column 97:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:

;
18:00:09  [IF - 0 row(s), 0.000 secs]  [Error Code: 900, SQL State: 42000]  ORA-00900: invalid SQL statement
18:00:09  [ELSE - 0 row(s), 0.000 secs]  [Error Code: 900, SQL State: 42000]   
ORA-00900: invalid SQL statement
18:00:09  [END - 0 row(s), 0.000 secs]  [Error Code: 900, SQL State: 42000]  ORA-00900: invalid SQL statement
18:00:09  [END - 0 row(s), 0.000 secs]  [Error Code: 900, SQL State: 42000]  ORA-00900: invalid SQL statement
... 6 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.000/0.000 sec  [0 successful, 0 warnings, 6 errors]

Below is my code:

DECLARE a NUMBER;

BEGIN

  SELECT 1 
    INTO a 
    FROM FREC_EMAIL t
   WHERE t.FranchiseNo = '208254846' 
     AND t.ReportID = 1 
     AND t.id = 165;

  IF a=1 THEN

       UPDATE FREC_EMAIL
          SET email = 'blah@foo.com'
        WHERE FranchiseNo = '208254846' 
          AND ReportID = 1 
          AND ID = 165;

    ELSE

        INSERT INTO FREC_EMAIL
          (FranchiseNo, Email, ReportID)
         VALUES
           ('208254846', 'blah@foo.com', 1);

    END IF;
END;
+3
source share
5 answers

SQL, , PL/SQL, . SQL , , .

9i Oracle MERGE, SQL, "upsert".

MERGE INTO frec_email t
USING (SELECT  'blah@foo.com' as email
                ,  '208254846' as FranchiseNo
                , 1 as ReportID
                , 165 as ID 
       FROM dual ) s
ON (s.ID = t.ID)
WHEN MATCHED THEN
     UPDATE SET t.email = s.email
WHEN NOT MATCHED THEN
    INSERT (t.FranchiseNo, t.Email, t.ReportID)
    VALUES  (s.FranchiseNo, s.Email, s.ReportID)
/
+9

pl/sql :

=.... .....;

SQL% ROWCOUNT = 0 THEN ...... END IF;

0

MERGE ( upsert ). Oracle ( ) .

0

, Oracle, , documentation. , Tom Qte - .

!

0

, , , , merge::

BEGIN

  UPDATE FREC_EMAIL
     SET email = 'blah@foo.com'
   WHERE FranchiseNo = '208254846' 
     AND ReportID = 1 
     AND ID = 165;

  IF SQL%NOTFOUND THEN
    INSERT INTO FREC_EMAIL
          (FranchiseNo, Email, ReportID, ID)
      VALUES
          ('208254846', 'blah@foo.com', 1, 165);
  END IF;

END;
0
source

Source: https://habr.com/ru/post/1718334/


All Articles