Oracle无法执行dml操作,无法在查询中调用

时间:2018-07-18 10:57:34

标签: oracle plsql

CREATE OR REPLACE FUNCTION cash_out_ticket(party_id   IN softdev.casino_users.party_id%TYPE ,session_id IN softdev.bus_session.session_id%TYPE                       
    ) RETURN NUMBER AS ret_val NUMBER;  

    P_EXCHANGE_BET_CREDITS softdev.COUNTRY.EXCHANGE_BET_CREDITS%type;
    P_EXCHANGE_VALUE       softdev.COUNTRY.EXCHANGE_VALUE%type;
    p_reserved_funds       softdev.casino_users.credits%type;
    p_session_id           softdev.bus_session.session_id%type;
    p_session_close        softdev.bus_session.session_close%type;
    ticket_closed          exception;

    CURSOR cur_tkt_sess (party_id IN softdev.casino_users.party_id%TYPE,session_id IN softdev.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  
              --  ,bs.player_win * P_EXCHANGE_BET_CREDITS / P_EXCHANGE_VALUE AS possible_win  
              --  ,bs.house_win                                           AS odds  
                ,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.party_id, cash_out_ticket.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)  -- je li tiket stariji od 24h
             OR  
             (rec_tkt_sess.live_prematch != '0')  --je li live (1 live , 0 not live)
             OR  
             (rec_tkt_sess.time_p <SYSDATE)  -- je li utakmice počela
            ) 
            THEN  
                ret_val := 0; 
            ELSE  
             ret_val := rec_tkt_sess.cash_out* P_EXCHANGE_BET_CREDITS / P_EXCHANGE_VALUE;

             play_beting.end_of_ticket(cash_out_ticket.session_id ,rec_tkt_sess.cash_out* P_EXCHANGE_BET_CREDITS / P_EXCHANGE_VALUE ,cash_out_ticket.party_id );
             --this procedure do updates , does not return anything. And it need to be run when conditions are fulfilled.

            END IF;  

         ELSE     
          ret_val := -1;            

       END IF;  

       RETURN(ret_val); 
END cash_out_ticket;

我有这个函数cash_out_ticket,它通过游标,然后游标项通过IF语句运行(如果满足条件),它需要返回cash_out并运行过程end_of_ticket。 但是,当我从对偶中调用cash_out_ticket时,出现错误,我无法在查询中执行dml操作。

在我的情况下,dml操作位于end_of_ticket内。它不返回任何内容,只是对IN参数进行一些更新。

如果if条件合适,是否可以调用end_of_ticket?

1 个答案:

答案 0 :(得分:2)

如错误所述,由于调用过程中的DML(插入/更新/删除),您无法从查询中调用函数。所以你不能做:

select cash_out_ticket(42, 123) from dual;

ORA-14551: cannot perform a DML operation inside a query 

您可以从PL / SQL上下文中调用它,例如在一个匿名块中:

declare
  ret_val number;
begin
  ret_val := cash_out_ticket(party_id => 42 ,session_id => 123);
  -- do something with ret_val
end;
/

根据您计划在何处以及如何调用它,可以使用绑定变量来获取返回值。