Oracle Insert via Select from multiple tables where one table may not have a row

前端 未结 6 1673
野性不改
野性不改 2020-12-25 08:56

I have a number of code value tables that contain a code and a description with a Long id.

I now want to create an entry for an Account Type that references a number

6条回答
  •  天涯浪人
    2020-12-25 09:43

    Outter joins don't work "as expected" in that case because you have explicitly told Oracle you only want data if that criteria on that table matches. In that scenario, the outter join is rendered useless.

    A work-around

    INSERT INTO account_type_standard 
      (account_type_Standard_id, tax_status_id, recipient_id) 
    VALUES( 
      (SELECT account_type_standard_seq.nextval FROM DUAL),
      (SELECT tax_status_id FROM tax_status WHERE tax_status_code = ?), 
      (SELECT recipient_id FROM recipient WHERE recipient_code = ?)
    )
    

    [Edit] If you expect multiple rows from a sub-select, you can add ROWNUM=1 to each where clause OR use an aggregate such as MAX or MIN. This of course may not be the best solution for all cases.

    [Edit] Per comment,

      (SELECT account_type_standard_seq.nextval FROM DUAL),
    

    can be just

      account_type_standard_seq.nextval,
    

提交回复
热议问题