It's possible to write one line if's 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's possible to write it in one line, just like java. Like this:
IF ( i.DECISAO_AT = 'S') ? 'PA' : 'I' ;
Thanks!
You can write an IF ... ELSE ... END IF;
on one line as others have shown; but no, you cannot do what you suggested and write it "just like Java":
IF ( i.DECISAO_AT = 'S') ? 'PA' : 'I' ;
PL/SQL does not understand the Java ?
and :
syntax, and does not have its own ternary operators as such. You can only use what is described in the documentation. The closest thing to what I think you're asking, for this particular statement anyway, is probably a case
:
CASE WHEN i.DECISAO_AT = 'S' THEN 'PA' ELSE 'I' END
Or maybe a decode
, as xQbert already suggested in a comment. Neither is an "IF" any more though.
There is no problem in PL/SQL
executing the code if it was in one line or several lines . What it matters the syntax is correct.
You could use the DECODE
statement that you can use inline:
The syntax for the DECODE function in Oracle/PLSQL is:
DECODE( expression , search , result [, search , result]... [, default] )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With