Oracle PLS-00103 错误。如何检查现有记录并根据该条件进行更新或插入?

发布于 2024-08-05 09:49:17 字数 1924 浏览 9 评论 0原文

我需要通过 SELECT 语句检查表中是否存在记录。如果记录存在,则进行更新,否则在表上创建记录。我正在尝试,但收到 PLS-00103 错误。

这些是我在 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]

以下是我的代码:

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 = '[email protected]'
        WHERE FranchiseNo = '208254846' 
          AND ReportID = 1 
          AND ID = 165;

    ELSE

        INSERT INTO FREC_EMAIL
          (FranchiseNo, Email, ReportID)
         VALUES
           ('208254846', '[email protected]', 1);

    END IF;
END;

I need to check if a record exists in the table or not from a SELECT statement. If the record exists, do an update otherwise create a record on the table. I'm trying to but i'm getting PLS-00103 error.

These are the errors that I'm getting 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]

The following 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 = '[email protected]'
        WHERE FranchiseNo = '208254846' 
          AND ReportID = 1 
          AND ID = 165;

    ELSE

        INSERT INTO FREC_EMAIL
          (FranchiseNo, Email, ReportID)
         VALUES
           ('208254846', '[email protected]', 1);

    END IF;
END;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小忆控 2024-08-12 09:49:17

我们应该尽可能使用 SQL,除非绝对必要,否则应避免使用 Pl/SQL。 SQL 语句执行速度更快,通常需要更少的输入,并且更容易正确执行。

从 9i 开始,Oracle 提供了 MERGE,这是执行“upsert”语句的单个 SQL 语句。

MERGE INTO frec_email t
USING (SELECT  '[email protected]' 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)
/

We should always use SQL whenever possible, and avoid using Pl/SQL unless it is strictly necessary. SQL statements perform faster, they usually require less typing and they are easier to get right.

Since 9i Oracle has provided MERGE, a single SQL statement which executes an "upsert" statement.

MERGE INTO frec_email t
USING (SELECT  '[email protected]' 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)
/
千纸鹤带着心事 2024-08-12 09:49:17

在 pl/sql 块中,您可以执行以下操作:

update table set column=....
在哪里.....;

如果 SQL%ROWCOUNT = 0 那么
插入......
结束如果;

K

In a pl/sql block, you can do this:

update table set column=....
where.....;

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

K

御弟哥哥 2024-08-12 09:49:17

使用 MERGE 命令(有些人也称为 upsert)。 Oracle 的参考(带有示例)此处

Use the MERGE command (also called upsert by some). Oracle's reference (with example) here.

绝不服输 2024-08-12 09:49:17

顺便说一句,如果您是 Oracle 的新手,值得花时间掌握官方的 文档。虽然从哪里开始可能看起来很困难,但 Tom Kyte 的 路线图 是获取必读列表的好地方。

祝你好运!

On a side note, if you are new to Oracle, it is worth spending time getting to grips with the offical documentation. Although it might appear difficult where to start with it, Tom Kyte's Road Map is good place to get a list of must reads.

Good luck!

箹锭⒈辈孓 2024-08-12 09:49:17

我通常使用以下概念,我认为它比 merge 更具可读性::

BEGIN

  UPDATE FREC_EMAIL
     SET email = '[email protected]'
   WHERE FranchiseNo = '208254846' 
     AND ReportID = 1 
     AND ID = 165;

  IF SQL%NOTFOUND THEN
    INSERT INTO FREC_EMAIL
          (FranchiseNo, Email, ReportID, ID)
      VALUES
          ('208254846', '[email protected]', 1, 165);
  END IF;

END;

I usually use the following concept, which I think is more readable than merge::

BEGIN

  UPDATE FREC_EMAIL
     SET email = '[email protected]'
   WHERE FranchiseNo = '208254846' 
     AND ReportID = 1 
     AND ID = 165;

  IF SQL%NOTFOUND THEN
    INSERT INTO FREC_EMAIL
          (FranchiseNo, Email, ReportID, ID)
      VALUES
          ('208254846', '[email protected]', 1, 165);
  END IF;

END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文