【发布时间】: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