SQL>
SQL>
SQL> create table product(
2 product_id number(4) not null,
3 product_description varchar2(20) not null
4 );
Table created.
SQL>
SQL> insert into product values (1,'Java');
1 row created.
SQL> insert into product values (2,'Oracle');
1 row created.
SQL> insert into product values (3,'C#');
1 row created.
SQL> insert into product values (4,'Javascript');
1 row created.
SQL> insert into product values (5,'Python');
1 row created.
SQL>
SQL>
SQL> create table company(
2 product_id number(4) not null,
3 company_id NUMBER(8) not null,
4 company_short_name varchar2(30) not null,
5 company_long_name varchar2(60)
6 );
Table created.
SQL> insert into company values(1,1001,'A Inc.','Long Name A Inc.');
1 row created.
SQL> insert into company values(1,1002,'B Inc.','Long Name B Inc.');
1 row created.
SQL> insert into company values(1,1003,'C Inc.','Long Name C Inc.');
1 row created.
SQL> insert into company values(2,1004,'D Inc.','Long Name D Inc.');
1 row created.
SQL> insert into company values(2,1005,'E Inc.','Long Name E Inc.');
1 row created.
SQL> insert into company values(2,1006,'F Inc.','Long Name F Inc.');
1 row created.
SQL>
SQL>
SQL>
SQL> create table company_site(
2 site_no number(4) not null,
3 site_descr varchar2(20) not null
4 );
Table created.
SQL> insert into company_site values (1,'New York');
1 row created.
SQL> insert into company_site values (2,'Washington');
1 row created.
SQL> insert into company_site values (3,'Chicago');
1 row created.
SQL> insert into company_site values (4,'Dallas');
1 row created.
SQL> insert into company_site values (5,'San Francisco');
1 row created.
SQL>
SQL>
SQL> create table org_company_site(
2 company_id number(8) not null,
3 site_no number(4) not null
4 );
Table created.
SQL> insert into org_company_site values (1001,1);
1 row created.
SQL> insert into org_company_site values (1002,2);
1 row created.
SQL> insert into org_company_site values (1003,3);
1 row created.
SQL> insert into org_company_site values (1004,1);
1 row created.
SQL> insert into org_company_site values (1004,2);
1 row created.
SQL> insert into org_company_site values (1004,3);
1 row created.
SQL> insert into org_company_site values (1005,1);
1 row created.
SQL> insert into org_company_site values (1005,4);
1 row created.
SQL> insert into org_company_site values (1005,5);
1 row created.
SQL> insert into org_company_site values (1006,1);
1 row created.
SQL>
SQL> create or replace function f_cursor_exp_complex return NUMBER
2 is
3 TYPE rc is REF CURSOR;
4
5 CURSOR myCursor IS
6 SELECT h.product_description,
7 CURSOR(SELECT o.company_long_name,
8 CURSOR (SELECT s.site_descr
9 FROM org_company_site os,company_site s
10 WHERE os.site_no =s.site_no
11 AND os.company_id =o.company_id)as site_name
12 FROM company o
13 WHERE o.product_id =h.product_id)long_name
14 FROM product h;
15
16 myRecord rc;
17 company_rec rc;
18 v_product_description VARCHAR2(20);
19 v_company_long_name VARCHAR2(60);
20 v_site_name VARCHAR2(20);
21 BEGIN
22
23 OPEN myCursor;
24 LOOP
25
26 FETCH myCursor INTO v_product_description,myRecord;
27 EXIT WHEN myCursor%notfound;
28 LOOP
29
30 FETCH myRecord INTO v_company_long_name,company_rec;
31 EXIT WHEN myRecord%notfound;
32 LOOP
33 FETCH company_rec INTO v_site_name;
34 EXIT WHEN company_rec%notfound;
35 DBMS_OUTPUT.PUT_LINE(v_product_description ||''||v_company_long_name||''||v_site_name);
36 END LOOP;
37 END LOOP;
38 END LOOP;
39
40 close myCursor;
41 RETURN (0);
42 EXCEPTION WHEN OTHERS THEN
43 RETURN (SQLCODE);
44 END;
45 /
Function created.
SQL>
SQL> drop table company;
Table dropped.
SQL>
SQL> drop table product;
Table dropped.
SQL>
SQL> drop table company_site;
Table dropped.
SQL>
SQL> drop table org_company_site;
Table dropped.
SQL>
|