oracle函数未返回正确值

时间:2018-08-06 12:22:43

标签: oracle plsql

我具有此功能,其中select中的数据需要与我在IF中所做的条件进行比较,并且在我在select中有一行的情况下可以使用,然后返回ret_val = 0。

但是,如果有两行或更多行,其中一个满足条件,而其中一个不满足,它仍然不返回0,则返回rec_tkt_sess.cash_out。

有人可以说出为什么在这些数据中IF中的游标不返回0吗?

就像它的光标不会通过所有行的IF ...

enter image description here

CREATE OR REPLACE FUNCTION cash_out_ticket_cond(party_id   IN casino_users.party_id%TYPE ,session_id IN bus_session.session_id%TYPE    

    ) RETURN NUMBER AS ret_val NUMBER; 

    P_EXCHANGE_BET_CREDITS COUNTRY.EXCHANGE_BET_CREDITS%type;
    P_EXCHANGE_VALUE    COUNTRY.EXCHANGE_VALUE%type;


CURSOR cur_tkt_sess (party_id IN casino_users.party_id%TYPE,session_id IN .bus_session.session_id%TYPE)
        IS
        SELECT bs.session_id 
                ,tii.status 
                ,ROW_NUMBER() OVER (PARTITION BY bs.session_id ORDER BY status ASC)  rn 
                ,NVL(TO_CHAR(bs.started, 'DD.MM.YYYY HH24:MI'), 'Live')  started 
                ,bs.bet  bet 
                ,tii.time_p 
                ,tii.live_prematch 
                ,cash_out(bet)  cash_out 
            FROM bus_session bs 
                ,ticket_items tii 
        WHERE bs.session_id             = tii.bus_session_session_id 
            AND bs.session_type         = 'TICKET SESSION' 
            AND bs.party_id             = cur_tkt_sess.party_id 
            AND bs.session_id           = cur_tkt_sess.session_id 
            AND NVL(bs.session_close, 'N') = 'N';           
    rec_tkt_sess cur_tkt_sess%ROWTYPE; 

    BEGIN       
        CHAGE_CREDITS (party_id, P_EXCHANGE_BET_CREDITS, P_EXCHANGE_VALUE);     
    OPEN cur_tkt_sess(cash_out_ticket_cond.party_id, cash_out_ticket_cond.session_id); 
            FETCH cur_tkt_sess 
    INTO rec_tkt_sess; 

    IF(cur_tkt_sess%FOUND) THEN         
        IF( 
            (TO_DATE(rec_tkt_sess.started,'DD.MM.YYYY HH24:MI:SS') +1 <=SYSDATE)  -- is ticket older then 24 hours
            OR 
            (rec_tkt_sess.live_prematch != '0')  --is it live  (1 live , 0 not live)
            OR 
            (rec_tkt_sess.time_p <SYSDATE)  -- is game begin
            )
            THEN 
                ret_val := 0;
            ELSE 
            ret_val := rec_tkt_sess.cash_out;           
        END IF; 

    ELSE    
        ret_val := -1; 
    END IF; 

    RETURN(ret_val); 

    END cash_out_ticket_cond;

1 个答案:

答案 0 :(得分:2)

您仅捕获一行,而没有ORDER BY子句,则您不知道将对两个示例行中的哪一个进行CHECHED。添加ORDER BY之后,循环遍历结果,直到达到所需条件:

OPEN cur_tkt_sess;
LOOP
    FETCH cur_tkt_sess INTO rec_tkt_sess;
    EXIT WHEN cur_tkt_sess%NOTFOUND;
    --[ Check some stuff, Do some stuff ]--
END LOOP
CLOSE cur_tkt_sess