Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Exists PLS-00405: subquery not allowed in this context

I have cursor it selects from TableA then Fetch Loop that inserts into TableB.

I want to check if the value already exists in the TableB.

If it exists then I want to skip the insert.

create or replace
PROCEDURE DAILY_RPT (
    v_start       IN DATE,
    v_end        IN DATE)

IS

    ao_out_no   out_pair.out_no%type;

cursor get is
SELECT ao_out_no from tableA;

BEGIN
 open get;
     LOOP
     fetch get into ao_out_no;
      EXIT WHEN get%NOTFOUND;
     if (ao_out_no = (select out_no from TableA where out_no = ao_out_no) THEN
     --DO NOTHING
     else 
        INSERT INTO TABLEB(OUT_NO) VALUES (ao_out_no);
     end if;

        END LOOP;
     close get;

END;

I used IF CONDITION however, I used variable into if condition & I am getting below.

PLS-00405: subquery not allowed in this context

if (ao_out_no = (select out_no from TableA where out_no = ao_out_no) THEN
like image 865
user206168 Avatar asked Nov 24 '25 06:11

user206168


2 Answers

You don't need cursor or PL/SQL at all:

INSERT INTO TABLEB(OUT_NO) 
SELECT ao_out_no 
FROM tableA ta
WHERE ... -- filtering rows
  AND NOT EXISTS (SELECT * From TableB tb WHERE tb.OUT_NO = ta.ao_out_no);
like image 181
Lukasz Szozda Avatar answered Nov 25 '25 19:11

Lukasz Szozda


Use the following :

for i in (
    select out_no from TableA where out_no
)
loop
    if i.out_no = ao_out_no
    then
        -- DO NOTHING
    else 
        ...

or create a new variable named x, and then assign a value to it by

select out_no into x from TableA where out_no = ao_out_no;

and check returning value for x.

like image 43
coco Avatar answered Nov 25 '25 19:11

coco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!