Question

i am trying to fill a variable in PL/SQL with an if-condition value like this:

v_variable := if(4 > 3) THEN 'A' ELSE 'B' END;

but this doesnt work.

Are there any options to do this?

Was it helpful?

Solution

Instead of doing this with an if, I'd go with case:

 v_variable := case when 4>3 then 'A' else 'B' end;

OTHER TIPS

Try this:

IF (4 > 3) THEN 
  v_variable :='A'; 
ELSE
  v_variable :='B'; 
END IF;

You can not assign If condition value in PL/SQL. your assignment is work in Python language but not in PL/SQL so you can avoid to this way and use IF condition in main logic block (BEGIN block).

DECLARE
   v_variable VARCHAR2 := 'B';
BEGIN

   IF 4 > 3 THEN
      v_variable := 'A';
   END IF;

   -- display the values of v_variable
   DBMS_OUTPUT.PUT_LINE ('v_variable value = '|| v_variable);
END;

You can use CASE:

SQL> DECLARE
  2     v_variable VARCHAR2(1);
  3  BEGIN
  4     v_variable := CASE WHEN 4 > 3 THEN 'A' ELSE 'B' END;
  5     dbms_output.put_line(v_variable);
  6  END;
  7  /
A
PL/SQL procedure successfully completed
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top