【问题标题】:Select Case Statement with an Insert选择带有插入的案例语句
【发布时间】:2020-09-21 12:06:37
【问题描述】:

由于我的 SQL,我有点困惑。我正在尝试使用“Select Case Statement”来验证是否存在特定关联,当存在时它不应该执行插入语句。但是,我不能在“Then”子句中使用插入语句。我做错了什么?

declare
cursor persons is
    (Select Person_id
     From PERSON_Table2Association_table p2c
     Where P2C.ID in
        (Select id
        From Association_table
        Where codepath like '&Old_Code_Path'
        and tree_id = 10
    );
new_codepath VARCHAR2(128) := '&New_Code_Path';
Begin
For person in persons
LOOP
select case
    when not exists(Select Person_id
                    From PERSON_Table2Association_table p2c
                    Where P2C.ID in
                        (Select ctn.id
                         From Association_table ctn
                         Where codepath like '&New_Code_Path2'
                         and tree_id = 10)
                         and person_id = person.person_id
                   )
    then (insert into PERSON_Table2Association_table -- here appears the error
          Values ((Select ENTITYID.getnextval from dual),
          0,
          (Select id From Association_table Where codepath = new_codepath and tree_id = 10),
          person.person_id,
          9999,
          null,
          'ACCESS')
         );
    else DBMS_OUTPUT.put_line('Person has already this association')
END LOOP;
COMMIT;
END;

我希望你能帮助我。谢谢。

【问题讨论】:

  • 请提供样本数据和期望的结果。解释你想要实现的逻辑。查询数据库时很少需要游标。
  • 看看这个问题,你应该得到答案:stackoverflow.com/questions/13503408/…
  • 目前您的 CASE 是 Select 列表中的 case expression,但您可能需要 case statement(删除 Select 关键字)docs.oracle.com/cd/B19306_01/appdev.102/b14261/… .恕我直言,根本不需要使用光标,这是一个简单的 MERGE。

标签: sql oracle select-case


【解决方案1】:

我相信您想要的解决方案类似于下面的代码

我纠正的一些语法问题是

  1. 光标声明末尾缺少括号
  2. 删除了循环开头的SELECT,并将其更改为IF...THEN...ELSE
  3. DBMS_OUTPUT.PUT_LINE 的末尾添加了;
  4. 已添加END IF
    DECLARE
        CURSOR persons IS
            (SELECT Person_id
               FROM PERSON_Table2Association_table p2c
              WHERE P2C.ID IN (SELECT id
                                 FROM Association_table
                                WHERE codepath LIKE '&Old_Code_Path' AND tree_id = 10));
        new_codepath   VARCHAR2 (128) := '&New_Code_Path';
    BEGIN
        FOR person IN persons
        LOOP
            IF NOT EXISTS
                   (SELECT Person_id
                      FROM PERSON_Table2Association_table p2c
                     WHERE     P2C.ID IN (SELECT ctn.id
                                            FROM Association_table ctn
                                           WHERE codepath LIKE '&New_Code_Path2' AND tree_id = 10)
                           AND person_id = person.person_id)
            THEN
                INSERT INTO PERSON_Table2Association_table                     -- here appears the error
                     VALUES ((SELECT ENTITYID.getnextval FROM DUAL),
                             0,
                             (SELECT id
                                FROM Association_table
                               WHERE codepath = new_codepath AND tree_id = 10),
                             person.person_id,
                             9999,
                             NULL,
                             'ACCESS');
            ELSE
                DBMS_OUTPUT.put_line ('Person has already this association');
            END IF;
        END LOOP;
        COMMIT;
    END;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2023-04-11
    • 2012-11-07
    • 2020-12-13
    • 2011-06-30
    • 2019-08-08
    • 1970-01-01
    相关资源
    最近更新 更多