Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

an INTO clause is expected in this SELECT statement when working with PLSQL

Tags:

plsql

I am getting the following error. Getting the error at SELECT CASE statement.

PLS-00428: an INTO clause is expected in this SELECT statement

FOR REMS IN cur_names LOOP

SELECT CASE   
        WHEN EXISTS (SELECT 1 
                     FROM SupplyTable 
                     WHERE FirstName = REMS.Names) 
        THEN 'Y' 
        ELSE 'N' 
    END AS rec_exists
FROM dual;

  IF rec_exists = 'Y' THEN
    FName := REMS.Names;
  ELSE
    FName := 'Gen';
  END IF;
END LOOP;
like image 873
user2281858 Avatar asked Jun 01 '16 11:06

user2281858


People also ask

Why is my SELECT statement not working in PLSQL?

In PLSQL block, columns of select statements must be assigned to variables, which is not the case in SQL statements. The second BEGIN's SQL statement doesn't have INTO clause and that caused the error.

Which select statement was executed without the INTO clause in PL/SQL?

A SELECT statement was executed without INTO clause in a PL/SQL block. Add the INTO clause to the SELECT statement. ORA-06550: PLS-00428: an INTO clause is expected in this SELECT statement

How do I run a SELECT statement in PL/SQL?

In PL/SQL you can't run standard (SQL) SELECT statements. You can run SELECT statements that return values to PL/SQL variables (either scalar variables or collections, such as arrays of records, etc.) If you have a SELECT statement in a PL/SQL block, you must specify the variable (s) INTO which the values are to be returned.

What is SELECT INTO clause in SQL Server?

SELECT - INTO Clause (Transact-SQL) SELECT...INTO creates a new table in the default filegroup and inserts the resulting rows from the query into it. To view the complete SELECT syntax, see SELECT (Transact-SQL).


2 Answers

you can´t have a random select inside your pl sql code. It must be in a loop, cursor, with an into clause. Just include an into clause here.

declare 
   v_value varchar2(1);
begin
...
FOR REMS IN cur_names LOOP

   SELECT CASE   
           WHEN EXISTS (SELECT 1 
                        FROM SupplyTable 
                        WHERE FirstName = REMS.Names) 
           THEN 'Y' 
           ELSE 'N' 
       END AS rec_exists
   INTO v_value 
   FROM dual;
   IF v_value = 'Y' THEN
      FName := REMS.Names;
   ELSE
      FName := 'Gen';
   END IF;
END LOOP;
...

or a loop

FOR REMS IN cur_names LOOP

   for i in 
   (   
       SELECT CASE   
           WHEN EXISTS (SELECT 1 
                        FROM SupplyTable 
                        WHERE FirstName = REMS.Names) 
           THEN 'Y' 
           ELSE 'N' 
       END AS rec_exists
       FROM dual
   )
   loop
      IF i.rec_exists = 'Y' THEN
         FName := REMS.Names;
      ELSE
         FName := 'Gen';
     END IF;
   end loop;
END LOOP;
like image 98
SomeJavaGuy Avatar answered Oct 03 '22 19:10

SomeJavaGuy


You need to store the output somewhere , like a variable. REC_EXISTS is a column, so right after the select it no longer exists.

Try this:

declare rec_exists varchar2(1);;
FOR REMS IN cur_names LOOP
SELECT CASE   
        WHEN EXISTS (SELECT 1 
                     FROM SupplyTable 
                     WHERE FirstName = REMS.Names) 
        THEN 'Y' 
        ELSE 'N' 
    END into rec_exists
FROM dual;
  IF rec_exists = 'Y' THEN
    FName := REMS.Names;
  ELSE
    FName := 'Gen';
  END IF;
END LOOP;
like image 27
sagi Avatar answered Oct 03 '22 19:10

sagi