在oracle中更新行和列

时间:2013-09-04 11:24:51

标签: sql oracle oracle10g

可以将行与表的列相关联。( 我有一张表,其中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));

请就此提出建议?

可能出了什么问题..

3 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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);

答案 2 :(得分: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')