PL / SQL中MERGE INTO的对象DML

时间:2014-11-10 23:43:38

标签: database oracle plsql dml

更新的对象DML:

UPDATE DEPARTMENTS d
SET d=NEW OBJ_DEPARTMENT(1, 'Information Technology')
WHERE d.OBJECT_VALUE=(
    SELECT VALUE(d)
    FROM DEPARTMENTS d
    WHERE ROWNUM=1
);

插入的对象DML:

INSERT INTO DEPARTMENTS d
VALUES(NEW OBJ_DEPARTMENT(1, 'Information Technology'));

删除对象DML:

DELETE FROM DEPARTMENTS d
WHERE d.OBJECT_VALUE=NEW OBJ_DEPARTMENT(1, 'Information Technology');

MERGE INTO的Object DML的语法是什么,其中目标是对象表,而源是嵌套的对象表

CREATE OR REPLACE OBJECT TYPE OBJ_DEPARTMENT IS OBJECT(
    DEPARTMENT_ID INTEGER,
    DEPARTMENT VARCHAR2(2000)
);
/
CREATE TABLE DEPARTMENTS OF OBJ_DEPARTMENT(
    CONSTRAINT PK_DEPARTMENTS PRIMARY KEY(DEPARTMENT_ID)
);
/

1 个答案:

答案 0 :(得分:1)

创建一个新的OBJ_DEPARTMENT集合,在PL / SQL中填充它,并使用table运算符在SQL语句中访问该集合。

此示例显示现有行,信息技术,更改为Humand资源,以及插入新行Accounting。

INSERT INTO DEPARTMENTS d
VALUES(NEW OBJ_DEPARTMENT(1, 'Information Technology'));


CREATE OR REPLACE TYPE OBJ_DEPARTMENT_TAB IS TABLE OF OBJ_DEPARTMENT;
/

declare
    obj_departments obj_department_tab := obj_department_tab(
        obj_department(1, 'Human Resources'),
        obj_department(2, 'Accounting')
    );
begin
    merge into departments old_departments
    using
    (
        select * from table(obj_departments)
    ) new_departments
    on (old_departments.department_id = new_departments.department_id)
    when matched then update
        set old_departments.department = new_departments.department
    when not matched then insert
        values(new_departments.department_id, new_departments.department);
end;
/

select * from departments;

DEPARTMENT_ID   DEPARTMENT
-------------   ----------
1               Human Resources
2               Accounting