PLSQL CASE WHEN CONDITION

Arav picture Arav · Apr 6, 2011 · Viewed 15.4k times · Source

I have two queries.

Query 1.

Below PL/SQL is not working. I want to store the output into varable test1 and test2. It's saying ORA-00923: FROM keyword not found. Not sure what is wrong

DECLARE    
  file_id NUMBER(10) NOT NULL :=5;
  test1   varchar(100);
  test2   varchar(100);

BEGIN

  DBMS_OUTPUT.PUT_LINE('File Id: ' || file_id);

  SELECT table_name 
    INTO test1,
         (CASE owner
            WHEN 'SYS' THEN  'The owner is SYS'
            WHEN 'SYSTEM' THEN 'The owner is SYSTEM'
          END) 
    INTO test2 
    FROM all_tables 
   WHERE rownum <= 1;

END;

Query 2.

In PL/SQL if i just use the select statement without into clause it's not working. Is it a rule that i need to use into clause. The below one does not work. If i want to spool a output from PL/SQL program do i need to store the output of column into the variable and do a dbms_output?

DECLARE

  file_id  NUMBER(10) NOT NULL :=5;
  test1 varchar(100);
  test2 varchar(100);

BEGIN

  DBMS_OUTPUT.PUT_LINE('File Id: ' || file_id);

  SELECT table_name,
         CASE owner
           WHEN 'SYS' THEN 'The owner is SYS'
           WHEN 'SYSTEM' THEN 'The owner is SYSTEM'
         END
    FROM all_tables;

END;

Answer

Jeffrey Kemp picture Jeffrey Kemp · Apr 6, 2011

You only need one INTO clause in a PL/SQL query, for example:

SELECT table_name, CASE owner WHEN bla bla ... END
INTO test1, test2
FROM all_tables;