1

I'm trying to create a IF NOT EXISTS query in Oralce like this:

DECLARE
   cnt   NUMBER;
BEGIN
   SELECT COUNT(*) INTO cnt FROM MY.MYTABLE
    WHERE MYFIELD = 400;

   IF (cnt = 0)
   THEN
      SELECT 'NO ROWS' AS FAIL FROM DUAL;
   ELSE
      SELECT 'SOME ROWS' AS PASS FROM DUAL;
   END IF;
END;

but this fails for

ORA-06550: line 9, column 7:
PLS-00428: an INTO clause is expected in this SELECT statement
ORA-06550: line 11, column 7:
PLS-00428: an INTO clause is expected in this SELECT statement

Why, what is wrong with this query?

1
  • The error messages gave you all the info you needed: line 9, column 7 and line 11, column 7 Commented Feb 6, 2014 at 5:07

5 Answers 5

5

Use into to assign value to variable.

DECLARE
   cnt   NUMBER;
   res   NVARCHAR2(50);
BEGIN
   SELECT COUNT(*) INTO cnt FROM MY.MYTABLE
    WHERE MYFIELD = 400;

   IF (cnt = 0)
   THEN
      SELECT 'NO ROWS' INTO RES FROM DUAL;
   ELSE
      SELECT 'SOME ROWS' INTO RES FROM DUAL;
   END IF;
END;

Also you can directly assign value to variable like

IF (cnt = 0)
       THEN
          res := 'NO ROWS';
       ELSE
          res := 'SOME ROWS';
       END IF;

Best way is just using this sql

SELECT DECODE(COUNT(*), 0, 'NO ROWS','SOME ROWS')  FROM MY.MYTABLE
    WHERE MYFIELD = 400;
Sign up to request clarification or add additional context in comments.

Comments

2

I prefer this way of checking for existence:

declare
  dummy      varchar2(1);
begin
  select null into dummy
  from MY.MYTABLE
  where MYFIELD = 400
    and rownum = 1;

  dbms_output.put_line('some rows'); -- some rows
exception
  when no_data_found then
    dbms_output.put_line('no rows'); -- no rows
end;

in my opinion it is better because count() can work quite slow for big amounts of data.

Comments

1

As the error message indicates you need a variable into which you can select the value:

select 'NO ROWS' as fail into v_status from dual;

Of course, you need to previously declare the variable:

declare
    v_status varchar2(10);
...

But, it is probably easier to just assign the value to the variable:

v_status := 'NO ROWS'

Comments

1
SQL> create table t (x int);


SQL> insert into t values(1);


SQL> commit;

SQL> set serveroutput on
SQL> declare
  2   res varchar2(10);
  3  begin
  4    select nvl((select 'SOME ROWS' from t where x =1 and rownum <=1), 'NO ROWS')
  5    into res
  6    from dual;
  7    dbms_output.put_line(res);
  8  end;
  9  /
SOME ROWS                                                                       

SQL> declare
  2   res varchar2(10);
  3  begin
  4    select nvl((select 'SOME ROWS' from t where x = 2 and rownum <=1), 'NO ROWS')
  5    into res
  6    from dual;
  7    dbms_output.put_line(res);
  8  end;
  9  /
 NO ROWS

Comments

1

Sounds a little bit strange maybe, but I prefer not to use and rownum=1 but and rownum < 2; and never using exception as part of business logic.

declare
  cnt   NUMBER;
  res   NVARCHAR2(50);
begin
  select COUNT(*) 
  into cnt
  from MY.MYTABLE
  where MYFIELD = 400
    and rownum < 2;

  IF (cnt = 0) THEN
    RES := 'NO ROWS';
  ELSE
    RES :='SOME ROWS';
  END IF;
end;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.