Search code examples
sqldatabaseoracle-databasecursor

Why do we get error for checking string value in sql


I'm running this piece of code implemented with cursor but I get if statement error after running it

declare
  cursor cr is
   select idd,name from tes;
     begin
       for n in cr
         IF n.name = '' THEN
          insert into tes2 values(n.idd,'$$$');
         ELSE
          insert into tes2 values(n.idd,n.name);
         END IF;
    end loop;
    commit;
   end;

the error :

Encountered the symbol "IF" when expecting one of the following:

. ( * @ % & - + / at loop mod remainder rem .. || multiset

The type of my variable are varchar2 and I also tried if n.name is NULL , didn't work.


Solution

  • After for statement, you forgot to add loop. End loop is there but no starting of loop. Try after changing below portion

    for n in cr 
       loop
             IF n.name = '' THEN 
              insert into tes2 values(n.idd,'$$$'); 
             ELSE 
              insert into tes2 values(n.idd,n.name); 
             END IF; 
        end loop;