SQL Oracle - 关系表和对象表之间的数据传输

时间:2014-08-18 17:33:04

标签: sql oracle object transfer relational

我需要Oracle数据库中两个表之间的数据传输解决方案。 Source是关系类型,target是表的对象类型。这是一个例子:

源表(现有数据):

CREATE TABLE "HISTORY" (
  "ID" NUMBER, 
  "ID_PROCESS" NUMBER, 
  "ID_UNIT" NUMBER, 
  "PASS_DATE" DATE DEFAULT SYSDATE
);

CREATE UNIQUE INDEX "HISTORY_PK" ON "HISTORY" ("ID");
ALTER TABLE "HISTORY" ADD CONSTRAINT "HISTORY_PROCESS_FK1" 
  FOREIGN KEY ("ID_PROCESS") REFERENCES "PROCESS" ("ID");
ALTER TABLE "HISTORY" ADD CONSTRAINT "HISTORY_PROCESS_FK2" 
  FOREIGN KEY ("ID_UNIT") REFERENCES "UNITS" ("ID");

目标表(空):

CREATE OR REPLACE TYPE t_history AS OBJECT (
  "ID" NUMBER, 
  "ID_PROCESS" REF t_process, 
  "ID_UNIT" REF t_unit, 
  "PASS_DATE" DATE
);

CREATE TABLE o_history OF t_history (
  "ID" PRIMARY KEY);

1 个答案:

答案 0 :(得分:2)

假设您已经为流程和单位值创建了类型和对象表,您可以这样做:

INSERT INTO o_history
SELECT t_history(h.id, REF(p), REF(u), h.pass_date)
FROM history h
JOIN o_process p ON p.id_process = h.id_process
JOIN o_unit u ON u.id_unit = h.id_unit;

这将从历史记录表中获取ID和传递日期,并从其他表中获取匹配值的引用。

SQL Fiddle demo