2

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?

4 Answers 4

2

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

 v_variable := case when 4>3 then 'A' else 'B' end;
Sign up to request clarification or add additional context in comments.

1 Comment

This was the right hint for me. Except: Instead of := I had to use = (in Oracle SQL Developer).
1

Try this:

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

Comments

1

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;

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.