I am a little bit confused because of my SQL. I am trying to use the "Select Case Statement" to validate if a specific association exists, when existing it shouldn't do the insert statement. However, I can't use an insert statement in the "Then" clause. What am I doing wrong?
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;
I hope you can help me. Thank you.
I believe the solution you are intending would look something like the code below
Some of the syntax issues that I corrected were
SELECT
in the beginning of the loop and changed it to an IF...THEN...ELSE
;
at then end of your DBMS_OUTPUT.PUT_LINE
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.