如何解决单行子查询返回多行

时间:2015-11-15 20:54:35

标签: sql oracle sql-update nested-table

我有一张员工表,它包含工资表。我想给所有现有员工增加%10。我试图将所有员工的工资日期更新到特定日期,但我遇到了单行子查询的问题。

我的数据库是这样的:

CREATE TYPE TEMPORAL_VARCHAR AS OBJECT (
VALID_TIME_LOWER_BOUND DATE,
VALID_TIME_UPPER_BOUND DATE,
VALUE_PART VARCHAR2(50) );
CREATE TYPE TEMPORAL_NUMBER AS OBJECT (
VALID_TIME_LOWER_BOUND DATE,
VALID_TIME_UPPER_BOUND DATE,
VALUE_PART NUMBER );

CREATE TYPE NAME_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE ADDRESS_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE DEPARTMENT_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE MANAGER_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE SALARY_TYPE AS TABLE OF TEMPORAL_NUMBER;

CREATE TABLE EMPLOYEE (
SSN NUMBER primary key,
NAME NAME_TYPE,
ADDRESS ADDRESS_TYPE ,
BIRTH_DATE DATE,
MANAGER MANAGER_TYPE ,
DEPARTMENT DEPARTMENT_TYPE,
SALARY SALARY_TYPE
)
NESTED TABLE NAME STORE AS NAME_TABLE,
NESTED TABLE ADDRESS STORE AS ADDRESS_TABLE,
NESTED TABLE MANAGER STORE AS MANAGER_TABLE,
NESTED TABLE DEPARTMENT STORE AS DEPARTMENT_TABLE,
NESTED TABLE SALARY STORE AS SALARY_TABLE
;

如何解决这个问题?我试着这样做

UPDATE TABLE(
SELECT E.SALARY
FROM EMPLOYEE E
) SAL
SET SAL.VALID_TIME_UPPER_BOUND = '11.16.2015'
WHERE SAL.VALID_TIME_UPPER_BOUND = TO_DATE('12.31.9999','MM.DD.YYYY');

2 个答案:

答案 0 :(得分:0)

1它可以重复

第二次见sample这里

在您的代码中第3位,您需要将where条件带入选择

UPDATE TABLE(
  SELECT E.SALARY
    FROM EMPLOYEE E 
      WHERE ssn in (SELECT ssn FROM EMPLOYEE e
   WHERE to_date('01.01.2015','mm.dd.yyyy')  in (
      SELECT VALID_TIME_UPPER_BOUND FROM TABLE(e.salary)
     ) 
   )
) SAL
SET SAL.VALID_TIME_UPPER_BOUND =  to_date('01.01.9999','mm.dd.yyyy')

test data

INSERT INTO EMPLOYEE(SSN, salary) values (1, SALARY_TYPE ());
INSERT INTO EMPLOYEE(SSN, salary) values (2, SALARY_TYPE ());
INSERT INTO EMPLOYEE(SSN, salary) values (3, SALARY_TYPE ());

INSERT INTO TABLE(SELECT salary FROM EMPLOYEE 
   WHERE ssn = 1)
   VALUES (to_date('01.01.2005','mm.dd.yyyy'), to_date('01.01.2015','mm.dd.yyyy'), 1);

INSERT INTO TABLE(SELECT salary FROM EMPLOYEE 
   WHERE ssn = 2)
   VALUES (to_date('02.02.2005','mm.dd.yyyy'), to_date('02.02.2015','mm.dd.yyyy'), 2);


INSERT INTO TABLE(SELECT salary FROM EMPLOYEE 
   WHERE ssn = 3)
   VALUES (to_date('03.03.2005','mm.dd.yyyy'), to_date('03.03.2015','mm.dd.yyyy'), 3);

p.s你真的需要复杂性吗?

答案 1 :(得分:0)

我使用像这样的迭代解决了我的问题

BEGIN
  FOR employees IN (SELECT SSN FROM EMPLOYEE)
  LOOP
UPDATE TABLE(
SELECT E.SALARY
FROM EMPLOYEE E
WHERE E.SSN = employees.SSN
) SAL
SET SAL.VALID_TIME_UPPER_BOUND = '11.16.2015'
WHERE SAL.VALID_TIME_UPPER_BOUND = TO_DATE('12.31.9999','MM.DD.YYYY');
  END LOOP;
END;