选择存储过程中没有表的变量​​

时间:2015-11-28 06:20:24

标签: sql oracle stored-procedures

我将一些输入参数传递给存储过程并在tem上进行一些计算。最后,我需要检索一些变量。我不会在数据库中使用任何表。

如何在Oracle中没有任何表的情况下使用存储过程仅返回变量?

这是我的代码。

PROCEDURE SP_C3_PSTK_TAKING(
    SCUR                                   IN OUT        CUST_CUR,      
    V_OUTLET                               IN            VARCHAR2,
    V_DATEOF_STOCK_TAKING                  IN            DATE,
    V_PSTK_VALUE                           IN            NUMBER,
    V_PSTK_VALUE_TP_CARDS                  IN            NUMBER,
    V_ACTUAL_STOCK_SHRINKAGE               IN            NUMBER,
    V_NESCAFE_SHRINKAGE                    IN            NUMBER,
    V_ICE_CUBE_SHRINKAGE                   IN            NUMBER,
    V_TURN_OVER_FOR_THE_PERIOD             IN            NUMBER,
    V_TP_CARD_SALES_FOR_THE_PERIOD         IN            NUMBER,
    V_STOCK_TAKE_RECEIVED_DATE             IN            DATE,
    V_STOCK_TAKE_DONE_BY                   IN            VARCHAR2 ,
    )
IS 
    TMPSTRSQL                               CLOB;
    TMPSTRSQL1                              CLOB;
    V_ACTUAL_STOCK_SHRINKAGE_PER            NUMBER;
    V_TOTAL_SHORTAGE                        NUMBER;
    V_T_OVER_EXCLUDING_TEL_CARDS            NUMBER;
    V_SHORTAGEPER                           NUMBER;
    V_ALLOWANCE                             NUMBER;
    V_VARIATION                             NUMBER
    OUTLET                                   VARCHAR(100);
    DATEOF_STOCK_TAKING                      DATE;
    PSTK_VALUE                               NUMBER;
    PSTK_VALUE_TP_CARDS                      NUMBER;
    ACTUAL_STOCK_SHRINKAGE                   NUMBER;
    NESCAFE_SHRINKAGE                        NUMBER;
    ICE_CUBE_SHRINKAGE                       NUMBER;
    TURN_OVER_FOR_THE_PERIOD                 NUMBER;
    TP_CARD_SALES_FOR_THE_PERIOD             NUMBER;
    STOCK_TAKE_RECEIVED_DATE                 DATE;
    STOCK_TAKE_DONE_BY                       VARCHAR(100);




BEGIN
    OUTLET                                  :=    V_OUTLET    ;
    DATEOF_STOCK_TAKING                     :=    V_DATEOF_STOCK_TAKING    ;
    PSTK_VALUE                              :=    V_PSTK_VALUE    ;
    PSTK_VALUE_TP_CARDS                     :=    V_PSTK_VALUE_TP_CARDS    ;
    ACTUAL_STOCK_SHRINKAGE                  :=    V_ACTUAL_STOCK_SHRINKAGE    ;
    NESCAFE_SHRINKAGE                       :=    V_NESCAFE_SHRINKAGE    ;
    ICE_CUBE_SHRINKAGE                      :=    V_ICE_CUBE_SHRINKAGE    ;
    TURN_OVER_FOR_THE_PERIOD                :=    V_TURN_OVER_FOR_THE_PERIOD    ;
    TP_CARD_SALES_FOR_THE_PERIOD            :=    V_TP_CARD_SALES_FOR_THE_PERIOD    ;
    STOCK_TAKE_RECEIVED_DATE                :=    V_STOCK_TAKE_RECEIVED_DATE    ;
    STOCK_TAKE_DONE_BY                      :=    V_STOCK_TAKE_DONE_BY    ;

    V_ACTUAL_STOCK_SHRINKAGE_PER := V_ACTUAL_STOCK_SHRINKAGE + ((V_ACTUAL_STOCK_SHRINKAGE/ 100) * 29 ) ;
    V_TOTAL_SHORTAGE :=    V_ACTUAL_STOCK_SHRINKAGE_PER + V_NESCAFE_SHRINKAGE + V_ICE_CUBE_SHRINKAGE ;
    V_T_OVER_EXCLUDING_TEL_CARDS := V_TURN_OVER_FOR_THE_PERIOD + V_TP_CARD_SALES_FOR_THE_PERIOD ;
    V_SHORTAGEPER := V_TOTAL_SHORTAGE  * 100 / V_T_OVER_EXCLUDING_TEL_CARDS ;
    V_ALLOWANCE := (V_PSTK_VALUE /100 ) * 1 ;      

     TMPSTRSQL := ' ';       


     TMPSTRSQL1 :=    'select V_ACTUAL_STOCK_SHRINKAGE_PER,V_TOTAL_SHORTAGE,V_T_OVER_EXCLUDING_TEL_CARDS,V_SHORTAGEPER,V_ALLOWANCE from dual';

OPEN SCUR FOR TMPSTRSQL1 || TMPSTRSQL;
END    SP_C3_PSTK_TAKING;

2 个答案:

答案 0 :(得分:0)

我将一些输入参数传递给存储过程,我将对其进行一些计算。最后,我需要检索一些变量。在这里,我不会在数据库中使用任何表。

enter image description here

答案 1 :(得分:0)

你的问题在于范围。在动态SQL中,我们将字符串传递给ref游标,该字符串必须在数据库引擎的上下文中可运行。换句话说,我们可以在SQL * Plus中运行此查询吗?

 select V_ACTUAL_STOCK_SHRINKAGE_PER,  
        V_TOTAL_SHORTAGE,
        V_T_OVER_EXCLUDING_TEL_CARDS,
        V_SHORTAGEPER,
        V_ALLOWANCE 
from dual

显然不是。 DUAL只有一列,DUMMY。但是我们可以从DUAL中选择任意数量的文字。

所以你需要做的是将该字符串转换为文字投影,如下所示:

 TMPSTRSQL1 := 'select '
               || to_char(v_ACTUAL_STOCK_SHRINKAGE_PER) ||','  
               || to_char(v_TOTAL_SHORTAGE) ||','
               || to_char(v_T_OVER_EXCLUDING_TEL_CARDS) ||','
               || to_char(v_SHORTAGEPER) ||','
               || to_char(v_ALLOWANCE) ||
        ' from dual';

    OPEN SCUR FOR TMPSTRSQL1 || TMPSTRSQL;
END    SP_C3_PSTK_TAKING;