是否有可能基于动态打开的ref游标的输出来创建视图或表?

时间:2019-03-31 21:42:31

标签: sql oracle plsql

我已经学习了如何返回动态打开的引用游标。现在,基于输出,我想创建一个表或视图。

输入表:

copy

记录

   create table sales
    (s_sale_name varchar2(20),
    s_date_sal date,
    s_qty number(10)
    );

查询输出

insert into sales values ('Norb','10-MAR-2019',10000);
insert into sales values ('Bert','10-MAR-2019',5000);
insert into sales values ('Alba','10-MAR-2019',4000);
insert into sales values ('Rob','10-MAR-2019',200000);
insert into sales values ('Norb','11-MAR-2019',5000);
insert into sales values ('Bert','11-MAR-2019',13000);
insert into sales values ('Rob','11-MAR-2019',80000);
insert into sales values ('Norb','12-MAR-2019',1000);
insert into sales values ('Bert','12-MAR-2019',4000);
insert into sales values ('Rob','12-MAR-2019',40000);
insert into sales values ('Alba','12-MAR-2019',2000);

现在,结果应保存在表或视图中。到目前为止,我已经学习了如何返回动态打开的引用游标。

------这是我使用的过程-----------

sales_name 10-MAR-2019 11-MAR-2019 12-MAR-2019 
Norb             10000        5000        1000
Bert              5000       13000        4000 
Alba              4000           0        2000 
Rob             200000       80000       40000

2 个答案:

答案 0 :(得分:1)

这确实是一个非常不错且具有挑战性的问题。我不同意SELECT part of a CREATE TABLE ... AS SELECT statement. Well we definitely can't do that上的@APC点。我认为,对于Oracle中的每个问题,都有一个解决方案。

您可以使用NESTED TABLE来满足您的要求。见下文:

设置:

create  table sales
    (s_sale_name varchar2(20),
    s_date_sal date,
    s_qty number(10)
    );

 /     
insert into sales values ('Norb','10-MAR-2019',10000);
insert into sales values ('Bert','10-MAR-2019',5000);
insert into sales values ('Alba','10-MAR-2019',4000);
insert into sales values ('Rob','10-MAR-2019',200000);
insert into sales values ('Norb','11-MAR-2019',5000);
insert into sales values ('Bert','11-MAR-2019',13000);
insert into sales values ('Rob','11-MAR-2019',80000);
insert into sales values ('Norb','12-MAR-2019',1000);
insert into sales values ('Bert','12-MAR-2019',4000);
insert into sales values ('Rob','12-MAR-2019',40000);
insert into sales values ('Alba','12-MAR-2019',2000);

---Created an Object of Sales table to hold intermediate result
create or replace  type  sales_obj is OBJECT
    (obj_sale_name varchar2(20),
     obj_date_sal date,
     obj_qty number(10)
    );
/
-- Table of Sales Object.
create or replace type vtest1Tab is table of sales_obj;
/

要创建表ccc的匿名块:

DECLARE 
 VAR VTEST1TAB ; 
 vsql varchar2(500);

BEGIN 

  vsql := 'create table ccc(col1) NESTED TABLE COL1 STORE AS TAB1 
           as 
           Select cast(multiset(Select * from SALES) as VTEST1TAB )
           from dual           
          ';    

  Execute immediate vsql ; 

END;

输出:

SQL> Select p.* 
     from ccc c, 
     table(c.COL1) p ;

答案 1 :(得分:1)

在此link中,“ Zlatko Sirotic”的答复确切介绍了如何识别光标的列并进行打印。

查找“ dyn_fetch”,因为该软件包足够通用,因此可以与用于打印数据的任何查询一起使用。您可以使用相同的方法将数据插入动态创建的表中。