oracle decode 子句中的 sql 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5172083/
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
sql statement inside decode clause
提问by Victor
The decode works like this:
解码是这样工作的:
SELECT DECODE('col1', 'x', 'result1','y','result2') resultFinal
FROM table1;
It possible to accomplish this in sql:
可以在 sql 中完成此操作:
SELECT *
FROM (SELECT DECODE('col1', 'x' (someSql),'y',(someOthersql)) result
FROM table1)
So instead of result1 and result2 being fixed values, they would be sql statements. If not possible, how can I achieve the same result without a stored proc.
因此,不是 result1 和 result2 是固定值,而是 sql 语句。如果不可能,我如何在没有存储过程的情况下获得相同的结果。
EDIT: someSql and someOthersql are both complex queries with many joins returining many but same number of cols with same col names.
编辑: someSql 和 someOthersql 都是复杂的查询,有许多连接返回许多但相同数量的具有相同列名的列。
回答by a_horse_with_no_name
If someSqland someOthersqlreturn exactlyone row with one column, then this should work.
如果someSql和someOthersql返回恰好一行与一列,那么这应该工作。
The following works for me:
以下对我有用:
select decode(col, (select 'foo' from dual), (select 'bar' from dual)) from some table
回答by Lost in Alabama
I think you may need to create a PL/SQL procedure to handle the complex logic.
我认为您可能需要创建一个 PL/SQL 过程来处理复杂的逻辑。