0

行可以与表格的列一起更新。
我有一张表格,其中要为单个成员更新 3 个不同年份的金额

我使用了以下查询

update ahd
   set amount1=(select a.amnt1
                  from ahd inner join ahdtmp a on ahd.member_key=a.member_key
                 where ahd.date1=a.date1
                   and a.status='FALSE'
                   and a.member_key in (select distinct member_key
                                          from ahd))

当我执行这个

第 2 行出现错误:
ORA-01427:单行子查询返回多于一行

临时表是ahdtmp

create table ahdtmp(
member_key number(10),
date1 date,
amnt1 number(10,2),
amnt2 number(10,2),
date_amend date,
Status varchar2(10));

请就此提出建议?

可能出了什么问题。。

4

3 回答 3

1

这里的问题是子查询返回多个值。您必须对其进行过滤以返回单个记录。

update ahd
set amount1=(select a.amnt1
              from ahd inner join ahdtmp a on ahd.member_key=a.member_key
             where ahd.date1=a.date1
               and a.status='FALSE'
               and a.member_key ='user_specific_key')
于 2013-09-04T14:08:09.807 回答
1

可能你想要的是:

update ahd
set amount1=(select a.amnt1
              from ahdtmp a
             where ahd.member_key=a.member_key and
                   ahd.date1     =a.date1
               and a.status='FALSE')
where  member_key = 'some particlar member ey'
于 2013-09-04T12:10:43.607 回答
1

也许下面的脚本会很有用,是一个通用代码(它只是一个例子):

CREATE TABLE SOURCE(
  s_key int,
  s_name varchar2(100));

CREATE TABLE TARGET(
  t_key int,
  t_name varchar2(100));


INSERT INTO SOURCE VALUES(1,'A');
INSERT INTO SOURCE VALUES(2,'B');
INSERT INTO SOURCE VALUES(3,'C');
INSERT INTO SOURCE VALUES(4,'D');

INSERT INTO TARGET VALUES(1,'Z');
INSERT INTO TARGET VALUES(2,'Z');
INSERT INTO TARGET VALUES(3,'Z');
INSERT INTO TARGET VALUES(4,'Z');

/*HERE THE CODE*/   
MERGE
INTO    target
USING   (
        SELECT  s.s_name,s.s_key
        FROM    source s
        )
ON      (t_key = s_key)
WHEN MATCHED THEN
UPDATE
SET     t_name = s_name;

这里是尝试这个例子的链接(它工作正常)sql fiddle

我不确定,但您的查询将是这样的:

MERGE
INTO    ahd
USING   (
        SELECT a.amnt1
        FROM ahdtmp a
        )
ON      (ahd.member_key=a.member_key)
WHEN MATCHED THEN
UPDATE
SET     amount1 = amnt1
WHERE ahd.status='FALSE'
    AND ahd.member_key IN (SELECT DISTINCT member_key FROM ahd);
于 2013-09-04T12:46:57.063 回答