创建运行SELECT语句的存储过程以更新Oracle表

时间:2014-01-01 19:07:15

标签: sql oracle

为了优化.NET应用程序,我在Oracle DB上创建了一个自定义表,以避免每次应用程序查询数据库时都会出现代价高昂的JOIN。以下SQL用于初始填充此自定义表。由于用于填充自定义表的表不断变化,我需要确保我的自定义表保持最新。我相信我需要创建一个以预定频率运行的存储过程,该过程将执行以下操作:a)删除SELECT返回的记录集中不再存在的任何记录b)追加由记录集返回的记录集中存在的任何记录。 SELECT但在QL_ASSETS表中不存在,c)更新QL_ASSETS表和SELECT返回的记录集中存在的记录中的任何数据。一个简单的UPDATE查询是否可以实现这一点,或者只是更新QL_ASSETS表和SELECT返回的记录集中存在的记录中的数据有何不同?如果是后者,有关如何完成所有3项任务的任何建议?谢谢!

INSERT INTO QL_ASSETS (
   THIRD_PARTY_SERVICE_CLEAN,
   LOCATION_ACCOUNT_ID,
   LOCATION_ACCOUNT_NUMBER,
   PLI_NAME,
   PLI,
   STREET,
   CITY,
   STATE,
   ZIPCODE,
   COUNTRY,
   ULTIMATE_PARENT_ID,
   PARENT_ID,
   LOCATION_STREET,
   LOCATION_CITY,
   LOCATION_STATE,
   LOCATION_ZIPCODE,
   LOCATION_COUNTRY,       
   ULTIMATE_PARENT_ACCOUNT_NUMBER)            
(SELECT 
   B.THIRD_PARTY_SERVICE_CLEAN,
   A.LOCATION_ACCOUNT_ID,
   D.ACCOUNT_NUMBER AS LOCATION_ACCOUNT_NUMBER,
   F.NAME AS PLI_NAME,
   A.PLI,
   H.STREET,
   H.CITY,
   H.STATE,
   H.ZIPCODE,
   H.COUNTRY,
   D.ULTIMATE_PARENT_ID,
   D.PARENT_ID,
   G.STREET AS LOCATION_STREET,
   G.CITY AS LOCATION_CITY,
   G.STATE AS LOCATION_STATE,
   G.ZIPCODE AS LOCATION_ZIPCODE,
   G.COUNTRY AS LOCATION_COUNTRY,       
   E.ACCOUNT_NUMBER AS ULTIMATE_PARENT_ACCOUNT_NUMBER
FROM ASSET_PLUS A,
 QUARTERLY_LETTER_LOAD B,
 DATASTORE_PLI_MAPPING C,
 ACCOUNT D,
 ACCOUNT E,
 PROD_INT F,
 ADDRESS G,
 ADDRESS H 
WHERE B.THIRD_PARTY_SERVICE_CLEAN = C.DATASTORE_SVC_NAME 
AND C.PLI = A.PLI 
AND A.PRODUCT_ID = F.ROW_ID 
AND E.PRIMARY_ADDRESS_ID = H.ADDRESS_ID 
AND D.PRIMARY_ADDRESS_ID = G.ADDRESS_ID 
AND D.ULTIMATE_PARENT_ID = E.ACCOUNT_ID 
AND D.ACCOUNT_ID = A.LOCATION_ACCOUNT_ID

1 个答案:

答案 0 :(得分:1)

以下materialised view可在创建后立即使用,并会按照您提到的时间范围每天刷新(START WITH ....)。

您也可以手动刷新,例如

EXEC DBMS_REFRESH.REFRESH(name => 'YOUR_MAT_VIEW');

MV的DDL:

CREATE MATERIALIZED VIEW YOUR_MAT_VIEW
BUILD IMMEDIATE
REFRESH COMPLETE
START WITH TRUNC(SYSDATE)--(+?/24) ?- HOUR OF DAY
NEXT TRUNC(SYSDATE)+1--(+?/24)
SELECT 
   B.THIRD_PARTY_SERVICE_CLEAN,
   A.LOCATION_ACCOUNT_ID,
   D.ACCOUNT_NUMBER AS LOCATION_ACCOUNT_NUMBER,
   F.NAME AS PLI_NAME,
   A.PLI,
   H.STREET,
   H.CITY,
   H.STATE,
   H.ZIPCODE,
   H.COUNTRY,
   D.ULTIMATE_PARENT_ID,
   D.PARENT_ID,
   G.STREET AS LOCATION_STREET,
   G.CITY AS LOCATION_CITY,
   G.STATE AS LOCATION_STATE,
   G.ZIPCODE AS LOCATION_ZIPCODE,
   G.COUNTRY AS LOCATION_COUNTRY,       
   E.ACCOUNT_NUMBER AS ULTIMATE_PARENT_ACCOUNT_NUMBER
FROM ASSET_PLUS A,
 QUARTERLY_LETTER_LOAD B,
 DATASTORE_PLI_MAPPING C,
 ACCOUNT D,
 ACCOUNT E,
 PROD_INT F,
 ADDRESS G,
 ADDRESS H 
WHERE B.THIRD_PARTY_SERVICE_CLEAN = C.DATASTORE_SVC_NAME 
AND C.PLI = A.PLI 
AND A.PRODUCT_ID = F.ROW_ID 
AND E.PRIMARY_ADDRESS_ID = H.ADDRESS_ID 
AND D.PRIMARY_ADDRESS_ID = G.ADDRESS_ID 
AND D.ULTIMATE_PARENT_ID = E.ACCOUNT_ID 
AND D.ACCOUNT_ID = A.LOCATION_ACCOUNT_ID;

您可以从YOUR_MAT_VIEW进一步查询,就像普通的简单表格一样。