具有返回值的Oracle过程

时间:2014-05-15 07:20:22

标签: oracle procedure

我必须编写Oracle过程 - 存储过程应该将date作为输入并返回一个整数作为输出。存储过程文本如下:

DECLARE
   bbg_yest       NUMBER;    
   bbg_today      NUMBER;    
   drates_yest    NUMBER;    
   drates_today   NUMBER;    
   equity_yest    NUMBER;    
   equity_today   NUMBER;    
   index_yest     NUMBER;    
   index_today    NUMBER;    
   retval         INTEGER;    
   retval1        INTEGER;    
   retval2        INTEGER;    
   retval3        INTEGER;    
   retval4        INTEGER;
BEGIN                                                                --Check 1
   SELECT COUNT (*)
     INTO bbg_yest
     FROM Crd_Own.Bbg_Oiv v
    WHERE asof_date = TO_DATE ('$last_bus_day', 'mm/dd/yyyy');

   SELECT COUNT (*)
     INTO bbg_today
     FROM Crd_Own.Bbg_Oiv v
    WHERE asof_date = TO_DATE ('$price_date', 'mm/dd/yyyy');

     --Check 2

     SELECT COUNT (*)
       INTO drates_yest
       FROM pm_own.deposit_rates
      WHERE     asof_date = TO_DATE ('$last_bus_day', 'mm/dd/yyyy')
            AND currency_code IN (' USD', 'EUR', 'GBP', 'JPY ')
            AND TERM_WEEKS = 0
            AND MARKET = 'L'
   ORDER BY CURRENCY_CODE, TERM_MONTHS;

     SELECT COUNT (*)
       INTO drates_today
       FROM pm_own.deposit_rates
      WHERE     asof_date = TO_DATE ('$price_date', 'mm/dd/yyyy')
            AND currency_code IN (' USD', 'EUR', 'GBP', 'JPY ')
            AND TERM_WEEKS = 0
            AND MARKET = 'L'
   ORDER BY CURRENCY_CODE, TERM_MONTHS;

   --Check 3

   SELECT COUNT (*)
     INTO equity_yest
     FROM pm_own.BDVD_FORECAST_EQUITY
    WHERE asof_date = TO_DATE ('$last_bus_day', 'mm/dd/yyyy');

   SELECT COUNT (*)
     INTO equity_today
     FROM pm_own.BDVD_FORECAST_EQUITY
    WHERE asof_date = TO_DATE ('$price_date', 'mm/dd/yyyy');

   --Check 4

   SELECT COUNT (*)
     INTO index_yest
     FROM pm_own.BDVD_FORECAST_INDEX
    WHERE asof_date = TO_DATE ('$last_bus_day', 'mm/dd/yyyy');

   SELECT COUNT (*)
     INTO index_today
     FROM pm_own.BDVD_FORECAST_INDEX
    WHERE asof_date = TO_DATE ('$price_date', 'mm/dd/yyyy');

   SELECT ( (bbg_today - bbg_yest) * 100) / bbg_yest INTO retval1 FROM DUAL;

   SELECT ( (drates_today - drates_yest) * 100) / drates_yest
     INTO retval2
     FROM DUAL;

   SELECT ( (equity_today - equity_yest) * 100) / equity_yest
     INTO retval3
     FROM DUAL;

   SELECT ( (index_today - index_yest) * 100) / index_yest
     INTO retval4
     FROM DUAL;

   retval := 0;

   IF retval1 < 0 AND ABS (retval1) > 20
   THEN
      retval := 1;
   ELSIF retval2 < 0 AND ABS (retval2) > 20
   THEN
      retval := 2;
   ELSIF retval3 < 0 AND ABS (retval3) > 20
   THEN
      retval := 3;
   ELSIF retval4 < 0 AND ABS (retval4) > 20
   THEN
      retval := 4;
   END IF;

   DBMS_OUTPUT.put_line (retval);
END;

请正确调用存储过程,帮助我编写正确的程序。

3 个答案:

答案 0 :(得分:0)

未经测试的粗略代码片段如下

CREATE OR REPLACE PROCEDURE my_proc (p_param IN DATE, o_param OUT NUMBER)
IS
   bbg_yest       NUMBER;
   bbg_today      NUMBER;
   drates_yest    NUMBER;
   drates_today   NUMBER;
   equity_yest    NUMBER;
   equity_today   NUMBER;
   index_yest     NUMBER;
   index_today    NUMBER;
   retval         INTEGER;
   retval1        INTEGER;
   retval2        INTEGER;
   retval3        INTEGER;
   retval4        INTEGER;
BEGIN                                                                
--Check 1
   SELECT COUNT (*)
     INTO bbg_yest
     FROM Crd_Own.Bbg_Oiv v
    WHERE asof_date = TO_DATE ('$last_bus_day', 'mm/dd/yyyy');

   SELECT COUNT (*)
     INTO bbg_today
     FROM Crd_Own.Bbg_Oiv v
    WHERE asof_date = TO_DATE ('$price_date', 'mm/dd/yyyy');

     --Check 2

     SELECT COUNT (*)
       INTO drates_yest
       FROM pm_own.deposit_rates
      WHERE     asof_date = TO_DATE ('$last_bus_day', 'mm/dd/yyyy')
            AND currency_code IN (' USD', 'EUR', 'GBP', 'JPY ')
            AND TERM_WEEKS = 0
            AND MARKET = 'L'
   ORDER BY CURRENCY_CODE, TERM_MONTHS;

     SELECT COUNT (*)
       INTO drates_today
       FROM pm_own.deposit_rates
      WHERE     asof_date = TO_DATE ('$price_date', 'mm/dd/yyyy')
            AND currency_code IN (' USD', 'EUR', 'GBP', 'JPY ')
            AND TERM_WEEKS = 0
            AND MARKET = 'L'
   ORDER BY CURRENCY_CODE, TERM_MONTHS;

   --Check 3

   SELECT COUNT (*)
     INTO equity_yest
     FROM pm_own.BDVD_FORECAST_EQUITY
    WHERE asof_date = TO_DATE ('$last_bus_day', 'mm/dd/yyyy');

   SELECT COUNT (*)
     INTO equity_today
     FROM pm_own.BDVD_FORECAST_EQUITY
    WHERE asof_date = TO_DATE ('$price_date', 'mm/dd/yyyy');

   --Check 4

   SELECT COUNT (*)
     INTO index_yest
     FROM pm_own.BDVD_FORECAST_INDEX
    WHERE asof_date = TO_DATE ('$last_bus_day', 'mm/dd/yyyy');

   SELECT COUNT (*)
     INTO index_today
     FROM pm_own.BDVD_FORECAST_INDEX
    WHERE asof_date = TO_DATE ('$price_date', 'mm/dd/yyyy');

   SELECT ( (bbg_today - bbg_yest) * 100) / bbg_yest INTO retval1 FROM DUAL;

   SELECT ( (drates_today - drates_yest) * 100) / drates_yest
     INTO retval2
     FROM DUAL;

   SELECT ( (equity_today - equity_yest) * 100) / equity_yest
     INTO retval3
     FROM DUAL;

   SELECT ( (index_today - index_yest) * 100) / index_yest
     INTO retval4
     FROM DUAL;

   retval := 0;

   IF retval1 < 0 AND ABS (retval1) > 20
   THEN
      retval := 1;
   ELSIF retval2 < 0 AND ABS (retval2) > 20
   THEN
      retval := 2;
   ELSIF retval3 < 0 AND ABS (retval3) > 20
   THEN
      retval := 3;
   ELSIF retval4 < 0 AND ABS (retval4) > 20
   THEN
      retval := 4;
   END IF;

   o_param := retval;
END;

答案 1 :(得分:0)

创建函数而不是过程并使用DATE输入参数。 在de COUNT(*)查询中删除订单,如果您只选择count(*),则没有任何目的。

CREATE FUNCTION (last_bus_day DATE, price_date DATE)
RETURN INTEGER
IS

BEGIN
   SELECT COUNT (*)
   INTO bbg_yest
   FROM Crd_Own.Bbg_Oiv v
   WHERE asof_date = last_bus_day;

    SELECT COUNT (*)
    INTO bbg_today
    FROM Crd_Own.Bbg_Oiv v
    WHERE asof_date = price_date;

    etc.

    IF retval1 < 0 AND ABS (retval1) > 20
    THEN
      retval := 1;
    ELSIF retval2 < 0 AND ABS (retval2) > 20
    THEN
      retval := 2;
    ELSIF retval3 < 0 AND ABS (retval3) > 20
    THEN
      retval := 3;
    ELSIF retval4 < 0 AND ABS (retval4) > 20
    THEN
      retval := 4;
    END IF;

    RETURN retval;

END;

答案 2 :(得分:0)

或者你可以创建一个函数

CREATE OR REPLACE FUNCTION my_function (p_param IN DATE) return number
IS
  l_result number;
BEGIN
   ..calculations...
   return l_result;
END;