Oracle
 sql >> Database >  >> RDS >> Oracle

Selezionare l'istruzione REF oracolo

Create or replace type table1_Type as object (
 id integer,
 dateStart date,
 etc varchar2(20));
-- TYPE TABLE1_TYPE compiled

create table table1 of table1_type;
-- table TABLE1 created.

Create or replace type table2_type as object(
 id integer,
 items varchar2(30),
 datePurchased varchar2(20),
 table1_Ref REF table1_type);
-- TYPE TABLE2_TYPE compiled

create table table2 of table2_type;
--table TABLE2 created.

INSERT INTO table1 VALUES(table1_Type(1, SYSDATE, 'etc1...'));
INSERT INTO table1 VALUES(table1_Type(2, SYSDATE, 'etc2...'));

SELECT  REF(t)
FROM    table1 t
WHERE   id = 1;
-- [TST.TABLE1_TYPE]

DECLARE
    l_table_1_id_1  REF table1_Type;
    l_table_1_id_2  REF table1_Type;
BEGIN
    SELECT  REF(t)
    INTO    l_table_1_id_1
    FROM    table1 t
    WHERE   id = 1;

    SELECT  REF(t)
    INTO    l_table_1_id_2
    FROM    table1 t
    WHERE   id = 2;

    INSERT INTO table2 VALUES (21, 'item21', SYSDATE, l_table_1_id_1);
    INSERT INTO table2 VALUES (22, 'item22', SYSDATE, l_table_1_id_2);
END;
-- anonymous block completed

SELECT COUNT(*) FROM table1;
-- 2
SELECT COUNT(*) FROM table2;
-- 2

SELECT * FROM table1;
/*
1   2013-06-16 03:51:50 etc1...
2   2013-06-16 03:52:05 etc2...
*/

SELECT * FROM table2;
/*
21  item21  2013-06-16 04:06:26 [TST.TABLE1_TYPE]
22  item22  2013-06-16 04:06:26 [TST.TABLE1_TYPE]
*/

SELECT  *
FROM    table1 t1
JOIN    table2 t2
ON      REF(t1) = t2.table1_Ref;
/*
1   2013-06-16 03:51:50 etc1... 21  item21  2013-06-16 04:06:26 [TST.TABLE1_TYPE]
2   2013-06-16 03:52:05 etc2... 22  item22  2013-06-16 04:06:26 [TST.TABLE1_TYPE]
*/