我正在尝试创建新表或插入当前存在的表。我需要的数据包含在两个单独的地方。一个是表,我们称之为T,它包含一列数据,我们称之为HEXID。另一个是包含许多列的视图,其中只有三列与我的问题相关。我们将调用视图V和相关列ID,MID和NAME。正如我所说,我想创建一个新表(或新视图),它应该看起来像这样:
+---------+------+-------+--------+
| T.HEXID | V.ID | V.MID | V.NAME |
+---------+------+-------+--------+
| DATA | DATA | DATA | DATA |
+---------+------+-------+--------+
这里有大量数据。我们正在谈论1000行中的10行。我一直试图这样做几天,但SQL Developer似乎不想合作。所以,我想知道的是:
谢谢!
答案 0 :(得分:0)
CREATE TABLE tab1(id NUMBER, col1 NUMBER);
-- table TAB1 created.
CREATE TABLE tab2(id NUMBER, col1 NUMBER, col2 NUMBER, col3 NUMBER);
-- table TAB2 created.
INSERT ALL
INTO tab1(id, col1) VALUES (tab1_id, tab1_col1)
INTO tab2(id, col1, col2, col3) VALUES (tab2_id, tab2_col1, tab2_col2, tab2_col3)
SELECT 1 AS tab1_id
, 11 AS tab1_col1
, 1 AS tab2_id
, 21 AS tab2_col1
, 22 AS tab2_col2
, 23 AS tab2_col3
FROM DUAL
;
-- 2 rows inserted.
SELECT * FROM tab1;
-- 1 11
SELECT * FROM tab2;
-- 1 21 22 23
CREATE VIEW tab_vw AS
SELECT tab1.id AS tab1_id
, tab1.col1 AS tab1_col1
, tab2.id AS tab2_id
, tab2.col1 AS tab2_col1
, tab2.col2 AS tab2_col2
, tab2.col3 AS tab2_col3
FROM tab1
JOIN tab2
ON tab1.id = tab2.id
;
-- view TAB_VW created.
SELECT * FROM tab_vw;
-- 1 11 1 21 22 23
CREATE OR REPLACE TRIGGER tab_trg
INSTEAD OF INSERT ON tab_vw
FOR EACH ROW
BEGIN
INSERT INTO tab1(id, col1) VALUES (:new.tab1_id, :new.tab1_col1);
INSERT INTO tab2(id, col1, col2, col3) VALUES (:new.tab2_id, :new.tab2_col1, :new.tab2_col2, :new.tab2_col3);
END;
-- TRIGGER TAB_TRG compiled
INSERT INTO tab_vw
(
tab1_id
, tab1_col1
, tab2_id
, tab2_col1
, tab2_col2
, tab2_col3
)
SELECT 2 AS tab1_id
, 11 AS tab1_col1
, 2 AS tab2_id
, 21 AS tab2_col1
, 22 AS tab2_col2
, 23 AS tab2_col3
FROM DUAL
;
-- 1 rows inserted.
SELECT *
FROM tab_vw;
-- 1 11 1 21 22 23
-- 2 11 2 21 22 23