Search code examples
sqloracleoracle12cinsert-intoselect-into

Select Into Variable and use it into IF (oracle)


I got procedure where I want to insert value to variable from Col1:

Procedure insertX
IS
 var1 varchar2(100) := '';
 check_s1 : = 'select Col1 into '||var1||' from table1';    
begin
 EXECUTE IMMEDIATE check_s1;

  if var1 is null then
   ...
   else
   ...
  end if;    
end;

But when I execute it something goes wrong.

As I see select into had error.

How to insert the value to my Var1 and then use it in IF condition?


Solution

  • Wrong syntax. Should be

    SQL> set serveroutput on
    SQL> declare
      2    var1 varchar2(100);
      3    check_s1 varchar2(100) := 'select dummy from dual';
      4  begin
      5    execute immediate check_s1 into var1;
      6
      7    if var1 is null then
      8       dbms_output.put_line('var1 is null');
      9    else
     10       dbms_output.put_line('var1 = ' || var1);
     11    end if;
     12  end;
     13  /
    var1 = X
    
    PL/SQL procedure successfully completed.
    
    SQL>