oracle 在 COALESCE 中使用 SELECT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25311854/
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:27:54  来源:igfitidea点击:

Using SELECT inside COALESCE

sqloracleplsqloracle-sqldeveloper

提问by user3808188

How do I correct the following SQL code, specifically the COALESCE part?

如何更正以下 SQL 代码,特别是 COALESCE 部分?

insert into Stmt G (ID,blah,foo)
select
coalesce(SELECT ID FROM Stmt G WHERE G.CLAIMNO=C.CLNUMBER, select StmtSeq.nextval from dual),
c.blah,
d.foo
from claim c
left join d on ...;

I'm taking the ID from the Stmt table itself if the ClaimNo matches, otherwise creating a new one. Is this not allowed in SQL? How else can I write this statement?

如果 ClaimNo 匹配,我将从 Stmt 表本身获取 ID,否则创建一个新的。这在 SQL 中是不允许的吗?我还能怎么写这个声明?

I'm getting a "Missing Expression" error on the coalesce part right now.

我现在在合并部分收到“缺少表达式”错误。

回答by Patrick Hofman

You should place parenthesis around the selects:

您应该在selects周围放置括号:

coalesce( (SELECT ID FROM Stmt G WHERE G.CLAIMNO=C.CLNUMBER)
        , (select StmtSeq.nextval from dual)
        )