如何在PL-SQL中创建参数化物化视图

时间:2016-06-27 08:25:03

标签: oracle plsql oracle11g

我需要为下面的视图中的每个手动MV(Materalised View)刷新操作传递月份(字符串)。

如果可以,请告诉我如何实现这一目标?如果无法做到这一点,我还有其他选择吗?

CREATE MATERIALIZED VIEW my_warranty_orders
   AS SELECT w.order_id, w.line_item_id, o.order_date
   FROM warranty_orders w, orders o
   WHERE o.order_id = o.order_id
   AND o.sales_rep_id = 165
   AND O.order_month = p_argument --pass the month
   ;

2 个答案:

答案 0 :(得分:2)

使用常量值并每次使用不同的常量重新创建物化视图,而不是刷新它:

CREATE OR REPLACE MATERIALIZED VIEW my_warranty_orders
   AS SELECT w.order_id, w.line_item_id, o.order_date
      FROM   warranty_orders w
             INNER JOIN orders o
             ON ( o.order_id = o.order_id )
      WHERE  o.sales_rep_id = 165
      AND    o.order_month  = DATE '2016-06-01';

或创建另一个表并将其连接到物化视图中:

CREATE TABLE my_warranty_orders_month (
  month DATE PRIMARY KEY
);

INSERT INTO my_warranty_orders_month VALUES ( DATE '2016-06-01' );

CREATE MATERIALIZED VIEW my_warranty_orders
   AS SELECT w.order_id, w.line_item_id, o.order_date
      FROM   warranty_orders w
             INNER JOIN orders o
             ON ( o.order_id = o.order_id )
             INNER JOIN my_warranty_orders_month m
             ON ( o.order_month = m.month )
      WHERE  o.sales_rep_id = 165;

然后当你想改变它时:

UPDATE my_warranty_orders_month
SET month = DATE '2016-07-01';

并刷新物化视图。

答案 1 :(得分:0)

如何使用dbms_application_info.set_client_info或上下文变量值作为mview的参数,并在每次刷新mview时重置上下文/客户端信息