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> 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> 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_report(p_cursor SYS_REFCURSOR,p_title VARCHAR2)
2 RETURN NUMBER
3 IS
4 v_product_description VARCHAR2(20);
5 v_company_short_name VARCHAR2(30);
6 v_ret_code NUMBER;
7 BEGIN
8 BEGIN
9 dbms_output.put_line(p_title);
10
11 LOOP
12 FETCH p_cursor INTO v_product_description,v_company_short_name;
13 EXIT WHEN p_cursor%NOTFOUND;
14 dbms_output.put_line(rpad(v_product_description,20,' ')||' '||
15 rpad(v_company_short_name,30,' '));
16 END LOOP;
17 v_ret_code :=1;
18 EXCEPTION WHEN OTHERS THEN
19 v_ret_code :=SQLCODE;
20 END;
21 RETURN (v_ret_code);
22 END;
23 /
Function created.
SQL>
SQL> SELECT 'Report Generated on '||TO_CHAR(SYSDATE,'MM/DD/YYYY ') "Report1"
2 FROM DUAL
3 WHERE f_report(
4 CURSOR(SELECT h.product_description,o.company_short_name
5 FROM product h,company o
6 WHERE o.product_id =h.product_id
7 AND 1 < (SELECT count(os.site_no)
8 FROM org_company_site os
9 WHERE os.company_id =o.company_id)
10 ),
11 'List of Organizations located in more than one site'
12 )=1;
Report1
-------------------------------
Report Generated on 07/24/2008
1 row selected.
List of Organizations located in more than one site
Oracle D Inc.
Oracle E Inc.
SQL>
SQL> drop table company;
Table dropped.
SQL>
SQL> drop table product;
Table dropped.
SQL>
SQL> drop table org_company_site;
Table dropped.
SQL>