29

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

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?

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

user3808188
  • 557
  • 2
  • 8
  • 20
  • 1
    Which specific part of the query do you receive the error on? (Paste the exact error message.) – cdhowie Aug 14 '14 at 15:27

1 Answers1

56

You should place parenthesis around the selects:

coalesce( (SELECT ID FROM Stmt G WHERE G.CLAIMNO=C.CLNUMBER)
        , (select StmtSeq.nextval from dual)
        )
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325