oracle PL/SQL 中的一行 IF 条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30482789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:53:00  来源:igfitidea点击:

One line IF condition in PL/SQL

oracleif-statementplsql

提问by TCMSSS

It's possible to write one line if's in pl/sql? I'm just curious.

如果在 pl/sql 中,可以写一行吗?我只是好奇。

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:

而且我想知道是否可以像java一样将其写成一行。像这样:

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

Thanks!

谢谢!

回答by Alex Poole

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 ... ELSE ... END IF;像其他人一样在一行上写一个;但是不,您不能按照您的建议进行操作并“像 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:

PL/SQL 不了解 Java?:语法,因此没有自己的三元运算符。您只能使用文档中描述的内容。无论如何,对于这个特定的陈述,最接近我认为您要问的可能是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.

或者也许 a decode,正如 xQbert 已经在评论中建议的那样。不过,“IF”也不再是。

回答by Moudiz

There is no problem in PL/SQLexecuting the code if it was in one line or several lines . What it matters the syntax is correct.

PL/SQL如果代码是一行或几行,执行代码都没有问题。重要的是语法是否正确。

回答by Sebz

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

您可以使用DECODE可以内联使用的语句:Oracle/PLSQL 中 DECODE 函数的语法是:

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

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

回答by Gary_W

Sure you can, but down the road when you need to debug that if with several tests it will be a pain. Keep it on separate lines, thus easier to read, debug, comment and easier (and thus cheaper) for maintenance down the road. The next guy to work on it will thank you.

当然可以,但是当您需要调试时,如果进行多次测试,那将是一件痛苦的事情。将它放在不同的行上,这样更容易阅读、调试、评论,并且更容易(因此更便宜)在路上进行维护。下一个工作的人会感谢你。

I read in a book (I think it was "C elements of style") once: "beware of clever, clever kills"). Keep it simple and easy. Code for the guy after you that has to maintain your code.

我在一本书上读到过(我认为是“C 元素风格”)曾经:“小心聪明,聪明的杀戮”)。保持简单和容易。为您之后必须维护您的代码的人编写代码。

回答by Gayan Dasanayake

You can use DECODE together with a SELECT INTO as below. "my_var" would be a variable declared to hold the decoded value.

您可以将 DECODE 与 SELECT INTO 一起使用,如下所示。“my_var”将是一个声明为保存解码值的变量。

SELECT DECODE(i.DECISAO_AT, 'S', 'PA', 'I') INTO my_var FROM DUAL;