One Line IF Condition in PL / SQL

Can I write one line if in pl / sql? I'm just curious.

I want to write this snippet:

IF ( i.DECISAO_AT = 'S')
THEN 'PA'
ELSE 'I'
END IF;

      

And I want to know if it is possible to write it in one line, just like java. Like this:

IF ( i.DECISAO_AT = 'S') ? 'PA' : 'I' ;

      

Thank!

+3


source to share


4 answers


You can write IF ... ELSE ... END IF;

on one line, as others have shown; but no, you cannot do what you suggested and write "just like Java":

IF ( i.DECISAO_AT = 'S') ? 'PA' : 'I' ;

      

PL / SQL does not understand Java syntax ?

and :

and does not own the ternary operator as such. You can use what is described in the documentation . The closest thing to what I think you are asking for for this particular statement, anyway, might be case

:



CASE WHEN i.DECISAO_AT = 'S' THEN 'PA' ELSE 'I' END

      

Or maybe decode

, as xQbert already suggested in the comment. Also there is no "IF" though.

+6


source


There is no problem in PL/SQL

executing code if it was on one line or multiple lines. Importantly, the syntax is correct.



+3


source


You can use a statement DECODE

you can use inline: The syntax for the DECODE function in Oracle / PLSQL is:

DECODE( expression , search , result [, search , result]... [, default] )

+3


source


Of course you can, but down the road when you need to debug this if with multiple tests it will be a pain. Keep it on separate lines so it's easier to read, debug, comment on, and easier (and therefore cheaper) to service along the way. The next guy he works on will thank you.

I read in the book (I think it was "With elements of style") once: "Beware of smart, smart murders"). Keep it simple and lightweight. Code for the guy after you to maintain your code.

+2


source







All Articles