Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a parameterized Materialized View in PL-SQL

I need to pass month(string) with every manual MV(Materalised View) refresh action for the view below.

if that is possible please let me know how I can achieve this? If this is not possible, do I have any other options?

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
   ;
like image 1000
user881703 Avatar asked Mar 18 '26 03:03

user881703


2 Answers

Either use a constant value and re-create the materialized view each time with a different constant rather than refreshing it:

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';

or create another table and join that into the materialized view:

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;

then when you want to change it:

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

and refresh the materialized view.

like image 185
MT0 Avatar answered Mar 20 '26 20:03

MT0


How about using dbms_application_info.set_client_info or context variable value as the parameter to the mview and reset the context/client info every time the mview is going to be refreshed

like image 33
Praneeth Gudumasu Avatar answered Mar 20 '26 19:03

Praneeth Gudumasu