Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One line IF condition in PL/SQL

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!

like image 933
TCMSSS Avatar asked May 27 '15 12:05

TCMSSS


3 Answers

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.

like image 91
Alex Poole Avatar answered Oct 21 '22 05:10

Alex Poole


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.

like image 3
Moudiz Avatar answered Oct 21 '22 03:10

Moudiz


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] )

like image 3
Sebz Avatar answered Oct 21 '22 03:10

Sebz