civesuas_sine
civesuas_sine

Reputation: 119

Oracle cannot perform dml operations cant be called inside query

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;

I have this function cash_out_ticket which goes through cursor and then cursors items runs through IF statement if conditions are fulfilled it need to return cash_out and run procedure end_of_ticket. But when i call cash_out_ticket from dual i get error that i cannot perform dml operation inside query .

In my case dml operations are inside of end_of_ticket. It doesnt return anything just do some updates from IN parameters.

Is there way to call end_of_ticket when conditions from if are right?

Upvotes: 0

Views: 1948

Answers (1)

Alex Poole
Alex Poole

Reputation: 191285

As the error says, you can't call your function from a query because of the DML (insert/update/delete) in the procedure it calls. So you can't do:

select cash_out_ticket(42, 123) from dual;

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

You can call it from a PL/SQL context, e.g. in an anonymous block:

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

Depending on where and how you had planned to call it you could use a bind variable to retrieve the return value.

Upvotes: 2

Related Questions