在oracle中从存储过程内部查看临时表

时间:2015-02-09 13:47:30

标签: oracle

我创建了以下存储过程:

CREATE OR REPLACE PROCEDURE GetTPICostCenters
IS
   varCost_Center VARCHAR2(10);
           varPI_MAX_DATE DATE;
           varFD_MAX_DATE DATE; 

BEGIN   

   --OPEN costcenter_WFDB;
   FOR item IN (select distinct(cost_center) as cost_center from fd_cost_center_validation where cost_center in 
           (select distinct(cost_center) from pi_cost_center_validation))
   LOOP
   --FETCH costcenter_WFDB INTO varCost_Center;



   SELECT GREATEST(nvl(CC_APPROVE_DATE,DATE '0001-01-01') ,nvl(CC_CHANGE_DATE,DATE '0001-01-01') ,nvl(CC_TAX_VALIDATE_DATE,DATE '0001-01-01'),nvl(COST_POOL_DETER_DATE,DATE '0001-01-01') ,nvl(DESC_UPDATE_DATE,DATE '0001-01-01'),
   nvl(PRICING_METHOD_UPD_DATE,DATE '0001-01-01') ,nvl(INTER_861_LAST_MODIFY_DATE,DATE '0001-01-01')) INTO varPI_MAX_DATE
   FROM pi_cost_center_validation WHERE COST_CENTER=item.cost_center;

   SELECT GREATEST(nvl(CC_APPROVE_DATE,DATE '0001-01-01') ,nvl(CC_CHANGE_DATE,DATE '0001-01-01') ,nvl(CC_TAX_VALIDATE_DATE,DATE '0001-01-01'),nvl(COST_POOL_DETER_DATE,DATE '0001-01-01') ,nvl(DESC_UPDATE_DATE,DATE '0001-01-01'),
   nvl(PRICING_METHOD_UPD_DATE,DATE '0001-01-01') ,nvl(INTER_861_LAST_MODIFY_DATE,DATE '0001-01-01')) INTO varFD_MAX_DATE
   FROM fd_cost_center_validation WHERE S_ROWID IN 
       (SELECT S_ROWID FROM FD_COST_CENTER_VALIDATION FD GROUP BY COST_CENTER, S_ROWID
        HAVING S_ROWID = (SELECT MAX(S_ROWID) FROM FD_COST_CENTER_VALIDATION WHERE COST_CENTER=FD.COST_CENTER))
        AND COST_CENTER=item.cost_center;

   IF (varPI_MAX_DATE>varFD_MAX_DATE) THEN   
    INSERT INTO TPI_Data
    (COST_CENTER,COST_CENTER_SHORT_NAME,COST_POOL,COST_POOL_PER,SUB_COST_POLL_SHORT_DESC,DRIVER,
     DRIVER_PER,MARKUP_PERCENTAGE,MARKUP_BASE,PRICING,COST_POOL_B,COST_POOL_B_PER,COST_SUB_POOL_B,
     DRIVER_2,DRIVER_2_PER,MARKUP_PERCENTAGE_2,MARKUP_BASE_2,PRICING_2,COST_POOL_C,COST_POOL_C_PER,
     COST_SUB_POOL_C,DRIVER_3,DRIVER_3_PER,MARKUP_PERCENTAGE_3,MARKUP_BASE_3,PRICING_3,COST_POOL_D,
     COST_POOL_D_PER,COST_SUB_POOL_D,DRIVER_4,DRIVER_4_PER,MARKUP_PERCENTAGE_4,MARKUP_BASE_4,
     PRICING_4,COST_POOL_E,COST_POOL_E_PER,COST_SUB_POOL_E,DRIVER_5,DRIVER_5_PER,MARKUP_PERCENTAGE_5,
     MARKUP_BASE_5,PRICING_5     
    )
    SELECT COST_CENTER, COST_CENTER_SHORT_NAME,COST_POOL,COST_POOL_PER,
    SUB_COST_POLL_SHORT_DESC
    ,DRIVER,DRIVER_PER,
    MARKUP_PERCENTAGE,MARKUP_BASE,PRICING,COST_POOL_B,COST_POOL_B_PER,COST_SUB_POOL_B,DRIVER_2,DRIVER_2_PER,MARKUP_PERCENTAGE_2,
    MARKUP_BASE_2,PRICING_2,
    COST_POOL_C,COST_POOL_C_PER,COST_SUB_POOL_C,DRIVER_3,DRIVER_3_PER,MARKUP_PERCENTAGE_3,
    MARKUP_BASE_3,PRICING_3,
    COST_POOL_D,COST_POOL_D_PER,COST_SUB_POOL_D,DRIVER_4,DRIVER_4_PER,MARKUP_PERCENTAGE_4,
    MARKUP_BASE_4,PRICING_4,
    COST_POOL_E,COST_POOL_E_PER,COST_SUB_POOL_E,DRIVER_5,DRIVER_5_PER,MARKUP_PERCENTAGE_5,
    MARKUP_BASE_5,PRICING_5 FROM pi_cost_center_validation WHERE COST_CENTER=item.cost_center;   
   ELSE   
    INSERT INTO TPI_Data
    (COST_CENTER,COST_CENTER_SHORT_NAME,COST_POOL,COST_POOL_PER,SUB_COST_POLL_SHORT_DESC,DRIVER,
     DRIVER_PER,MARKUP_PERCENTAGE,MARKUP_BASE,PRICING,COST_POOL_B,COST_POOL_B_PER,COST_SUB_POOL_B,
     DRIVER_2,DRIVER_2_PER,MARKUP_PERCENTAGE_2,MARKUP_BASE_2,PRICING_2,COST_POOL_C,COST_POOL_C_PER,
     COST_SUB_POOL_C,DRIVER_3,DRIVER_3_PER,MARKUP_PERCENTAGE_3,MARKUP_BASE_3,PRICING_3,COST_POOL_D,
     COST_POOL_D_PER,COST_SUB_POOL_D,DRIVER_4,DRIVER_4_PER,MARKUP_PERCENTAGE_4,MARKUP_BASE_4,
     PRICING_4,COST_POOL_E,COST_POOL_E_PER,COST_SUB_POOL_E,DRIVER_5,DRIVER_5_PER,MARKUP_PERCENTAGE_5,
     MARKUP_BASE_5,PRICING_5     
    )
     SELECT COST_CENTER, COST_CENTER_SHORT_NAME,COST_POOL,COST_POOL_PER,
     SUB_COST_POLL_SHORT_DESC,DRIVER,DRIVER_PER,MARKUP_PERCENTAGE,MARKUP_BASE,PRICING,COST_POOL_B,
     COST_POOL_B_PER,COST_SUB_POOL_B,DRIVER_2,DRIVER_2_PER,MARKUP_PERCENTAGE_2,MARKUP_BASE_2,PRICING_2,
     COST_POOL_C,COST_POOL_C_PER,COST_SUB_POOL_C,DRIVER_3,DRIVER_3_PER,MARKUP_PERCENTAGE_3,MARKUP_BASE_3,PRICING_3,
     COST_POOL_D,COST_POOL_D_PER,COST_SUB_POOL_D,DRIVER_4,DRIVER_4_PER,MARKUP_PERCENTAGE_4,MARKUP_BASE_4,PRICING_4,
     COST_POOL_E,COST_POOL_E_PER,COST_SUB_POOL_E,DRIVER_5,DRIVER_5_PER,MARKUP_PERCENTAGE_5,MARKUP_BASE_5,PRICING_5 
     FROM FD_COST_CENTER_VALIDATION WHERE S_ROWID IN 
       (SELECT S_ROWID FROM FD_COST_CENTER_VALIDATION FD GROUP BY COST_CENTER, S_ROWID
        HAVING S_ROWID = (SELECT MAX(S_ROWID) FROM FD_COST_CENTER_VALIDATION WHERE COST_CENTER=FD.COST_CENTER))
        AND COST_CENTER=item.cost_center;
     END IF;
    END LOOP;  
   --CLOSE costcenter_WFDB;

   INSERT INTO TPI_Data
    (COST_CENTER,COST_CENTER_SHORT_NAME,COST_POOL,COST_POOL_PER,SUB_COST_POLL_SHORT_DESC,DRIVER,
     DRIVER_PER,MARKUP_PERCENTAGE,MARKUP_BASE,PRICING,COST_POOL_B,COST_POOL_B_PER,COST_SUB_POOL_B,
     DRIVER_2,DRIVER_2_PER,MARKUP_PERCENTAGE_2,MARKUP_BASE_2,PRICING_2,COST_POOL_C,COST_POOL_C_PER,
     COST_SUB_POOL_C,DRIVER_3,DRIVER_3_PER,MARKUP_PERCENTAGE_3,MARKUP_BASE_3,PRICING_3,COST_POOL_D,
     COST_POOL_D_PER,COST_SUB_POOL_D,DRIVER_4,DRIVER_4_PER,MARKUP_PERCENTAGE_4,MARKUP_BASE_4,
     PRICING_4,COST_POOL_E,COST_POOL_E_PER,COST_SUB_POOL_E,DRIVER_5,DRIVER_5_PER,MARKUP_PERCENTAGE_5,
     MARKUP_BASE_5,PRICING_5     
    )
     SELECT COST_CENTER, COST_CENTER_SHORT_NAME,COST_POOL,COST_POOL_PER,
     SUB_COST_POLL_SHORT_DESC,DRIVER,DRIVER_PER,MARKUP_PERCENTAGE,MARKUP_BASE,PRICING,COST_POOL_B,COST_POOL_B_PER,
     COST_SUB_POOL_B,DRIVER_2,DRIVER_2_PER,MARKUP_PERCENTAGE_2,MARKUP_BASE_2,PRICING_2,COST_POOL_C,COST_POOL_C_PER,
     COST_SUB_POOL_C,DRIVER_3,DRIVER_3_PER,MARKUP_PERCENTAGE_3,MARKUP_BASE_3,PRICING_3,COST_POOL_D,COST_POOL_D_PER,
     COST_SUB_POOL_D,DRIVER_4,DRIVER_4_PER,MARKUP_PERCENTAGE_4,MARKUP_BASE_4,PRICING_4,COST_POOL_E,COST_POOL_E_PER,
     COST_SUB_POOL_E,DRIVER_5,DRIVER_5_PER,MARKUP_PERCENTAGE_5,MARKUP_BASE_5,PRICING_5 FROM FD_COST_CENTER_VALIDATION 
     WHERE S_ROWID IN 
     (SELECT S_ROWID FROM FD_COST_CENTER_VALIDATION FD GROUP BY COST_CENTER, S_ROWID
        HAVING S_ROWID = (SELECT MAX(S_ROWID) FROM FD_COST_CENTER_VALIDATION WHERE COST_CENTER=FD.COST_CENTER))
        AND COST_CENTER NOT IN (SELECT COST_CENTER FROM TPI_Data);


        COMMIT;

END; 

在上面的存储过程中,TPI_Deata是在过程中填充的全局临时表。在sql server中我们曾经做过" select * from temp_table"在存储过程内部查看执行存储过程时的结果但是在这里我应该如何处理全局临时表,这样当我执行存储过程时,我得到表中的记录集?

2 个答案:

答案 0 :(得分:1)

使用Oracle时,您应该忘记MSSQL。他们的方法完全不同。如果您希望从存储过程返回一些表格数据,通常应使用out REF CURSOR参数。如果您只想调试数据,那么在调用SP后执行SELECT。

坏消息是,在12c中,Oracle实现了MSSQL-like facility来减轻ms开发人员的痛苦。

答案 1 :(得分:0)

完成此过程后,您可以查询表格。如果您看到零行,那可能是因为临时表是使用ON COMMIT DELETE ROWS(默认值)而不是ON COMMIT PRESERVE ROWS声明的。

http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_7002.htm#i2189569

如果您尝试从UI查看结果,则应返回引用游标或使用常规表,以便数据保持不变。在全局临时表中,一旦会话关闭,数据就会消失。