选择语句REF oracle [英] Select statement REF oracle

查看:88
本文介绍了选择语句REF oracle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建将使用引用的select语句时,我需要一些帮助.

I need some help with creating a select statement that will use a ref.

我设法很好地插入了值,但是当我尝试使用where语句提取值时,输出要么是数据类型错误,要么将输出两个表以及它们都包含的数据.

I managed to insert the values fine but when i try to extract the values with a where statement the output is either that the datatype is wrong or it will output the two tables and the data they both contain.

这只是一个例子:

Create or replace table1_Type as object {
 id integer,
 dateStart date,
 etc varchar2(20));
}
/

create table table1 of table1_type;

Create or replace table2_type as object
 id integer,
 items varchar2(30),
 datePurchased varchar2(20),
 table1_Ref REF table1_type);
/

create table table2 of table2_type;

所以我尝试了

so i tried

Select * from table2 a, table1 b where table1.id = table2.table1_ref
Select * from table2 a, table1 b where table1.id = deref(b.table1_ref)

不起作用.

which does not work.

我对此很陌生,如果我没有正确解释,请您谅解.我想做的是选择例如由table1/id购买的项目,因此输出应仅显示 通过特定ID购买的商品.

Iam new to this so sorry if i didnt explain properly. What i am trying to do is to select the items for example that were purchased by table1/id so the output should only display the items that were purchased by a certain id.

推荐答案

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]
*/

这篇关于选择语句REF oracle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆