首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >内联查询以更新多行

内联查询以更新多行
EN

Stack Overflow用户
提问于 2009-08-11 10:10:50
回答 3查看 4.7K关注 0票数 0

以下是我在Oracle 10g中使用的表的简要描述:

注:表: jnldetail :单行,数据如图所示。对于一个帐户,有多个包id附加到同一个bill_ref_no上。因此,我试图用多个package_id更新“jnl细节”。

index_bill_ref与bill_ref_no的关系: account_no与( index_bill_ref和bill_ref_no ):1-多的关系

代码语言:javascript
复制
**Table : jnldetail** :
account_no     bill_ref_no     amount

8594822        74282843        822

我使用以下命令添加了另一列package_id:

代码语言:javascript
复制
alter table jnldetail add package_id number(10)


**table: bill_invoice**:

account_no    bill_ref_no       index_bill_ref

8594822       74282843          763653495


**table: bill_invoice_detail**:

index_bill_ref      package_id    component_id

763653495           20000077      20000177

763653495           20000250      20000528

763653495           13000019      13000137

**Expected Result:**
**Table : jnldetail** :

account_no     bill_ref_no     amount       package_id

8594822        74282843        822          20000077

8594822        74282843        822          20000250

8594822        74282843        822          13000019 

我的问题是:

代码语言:javascript
复制
UPDATE jnldetail tp
SET tp.package_id = (
    select 
         t1.package_id 
    from bill_invoice_detail t1
       , bill_invoice t2
    where 
         t1.index_bill_ref = t2.index_bill_ref 
      and
         t2.account_no = tp.account_no
)

错误消息是: ora 01427 :单行子查询返回多行

任何投入都是有帮助的。

谢谢!

EN

回答 3

Stack Overflow用户

发布于 2009-08-11 10:20:48

问题是,您试图将tp.package_id设置为多个数字,因为您的子查询返回多个结果,例如20000077和13000019。您需要修改子查询,以便只返回一个值。

票数 1
EN

Stack Overflow用户

发布于 2009-08-11 10:23:11

当您准备好获取完整的数据时,为什么不将表分开并使用join呢?

票数 0
EN

Stack Overflow用户

发布于 2009-08-11 11:34:10

这很棘手,原因有二:

1)您希望更新现有行,并希望添加两个新行

2)这两个新行都需要原始jnl细节表( bill_invoice )和package_id表(package_id)的数据。

要处理1,您可以使用MERGE语句,但是由于使用了2,因此在MERGE语句的using子句中需要jnl细节。

下面是你的例子:

代码语言:javascript
复制
SQL> create table jnldetail (account_no, bill_ref_no, amount)
  2  as
  3  select 8594822, 74282843, 822 from dual
  4  /

Tabel is aangemaakt.

SQL> alter table jnldetail add package_id number(10)
  2  /

Tabel is gewijzigd.

SQL> create table bill_invoice (account_no, bill_ref_no, index_bill_ref)
  2  as
  3  select 8594822, 74282843, 763653495 from dual
  4  /

Tabel is aangemaakt.

SQL> create table bill_invoice_detail (index_bill_ref, package_id, component_id)
  2  as
  3  select 763653495, 20000077, 20000177 from dual union all
  4  select 763653495, 20000250, 20000528 from dual union all
  5  select 763653495, 13000019, 13000137 from dual
  6  /

Tabel is aangemaakt.

就像你描述的那样。

代码语言:javascript
复制
SQL> UPDATE jnldetail tp
  2     SET tp.package_id =
  3         ( select t1.package_id
  4             from bill_invoice_detail t1
  5                , bill_invoice t2
  6            where t1.index_bill_ref = t2.index_bill_ref
  7              and t2.account_no = tp.account_no
  8         )
  9  /
       ( select t1.package_id
         *
FOUT in regel 3:
.ORA-01427: single-row subquery returns more than one row

update语句失败,因为您试图将3行返回查询的结果分配给单个列。

以下是合并语句:

代码语言:javascript
复制
SQL> merge into jnldetail jd
  2  using ( select bi.account_no
  3               , bi.bill_ref_no
  4               , jd.amount
  5               , bid.package_id
  6               , row_number() over (partition by bi.account_no,bi.bill_ref_no,bi.index_bill_ref order by null) rn
  7            from bill_invoice bi
  8               , bill_invoice_detail bid
  9               , jnldetail jd
 10           where bi.index_bill_ref = bid.index_bill_ref
 11             and bi.account_no = jd.account_no
 12             and bi.bill_ref_no = jd.bill_ref_no
 13        ) bi
 14     on (   jd.account_no     = bi.account_no
 15        and jd.bill_ref_no    = bi.bill_ref_no
 16        and bi.rn             = 1
 17        )
 18   when matched then
 19        update set package_id = package_id
 20   when not matched then
 21        insert values (bi.account_no,bi.bill_ref_no,bi.amount,bi.package_id)
 22  /

3 rijen zijn samengevoegd.

请注意,我们选择要更新的任意行:具有rn = 1的行。

代码语言:javascript
复制
SQL> select * from jnldetail
  2  /

ACCOUNT_NO BILL_REF_NO     AMOUNT PACKAGE_ID
---------- ----------- ---------- ----------
   8594822    74282843        822   13000019
   8594822    74282843        822   20000077
   8594822    74282843        822   20000250

3 rijen zijn geselecteerd.

你好,罗伯。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1259479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档