Home > front end >  PLS-00103 error while executing a sql block (Encountered the symbol "SELECT" ...)
PLS-00103 error while executing a sql block (Encountered the symbol "SELECT" ...)

Time:03-24

I'm trying to learn PLSQL and this is a code that I been trying to execute, When I execute the code I get the following error.

ORA-06550: line 2, column 7:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:

   ( -   case mod new not null <an identifier>
   <a double-quoted delimited-identifier> <a bind variable>
   continue avg count current exists max min prior sql stddev
   sum variance execute forall merge time timestamp interval
   date <a string literal with character set specification>
   <a number> <a single-quoted SQL string> pipe
   <an alternatively-quoted string literal with character set specification>
   <an alternat
ORA-06550: line 2, column 58:
PLS-00103: Encountered the symbol ")" when expecting one of the following:

   * & -   ; / at for mod remainder rem <an exponent (**)> and
   or group having intersect minus order start union where
   connect || multiset

I know you can assign a variable to the select statement and use that variable in IF statement, I just want to know the error in the following code.

BEGIN
  IF((SELECT marks FROM students WHERE student_id ='s10')= 'Passed') THEN
        DBMS_OUTPUT.PUT_LINE('Hello!');
  END IF;
END;

CodePudding user response:

begin 
for i in (SELECT marks FROM students WHERE student_id ='s10') loop
    if i.marks = 'Passed' then 
        DBMS_OUTPUT.PUT_LINE('Hello!');
    end if;
end loop;
end;

OR with a variable if the student_id is unique

declare
v_mark students.marks%type;
begin 
SELECT marks into v_mark FROM students WHERE student_id ='s10';
    if v_mark = 'Passed' then 
        DBMS_OUTPUT.PUT_LINE('Hello!');
    end if;
end;

CodePudding user response:

Using a cursor:

declare
cursor c1 is SELECT marks FROM students WHERE student_id ='s10';
begin
for r1 in c1 loop
    if r1.marks = 'Passed' then 
        DBMS_OUTPUT.PUT_LINE('Hello!');
    end if; 
end loop; 
end;
  • Related