-1

I am novice in PL/SQL and i try to write a function that should read data from db control them and return a number, but it generates an error ORA-00900: invalid SQL statement.

FUNCTION get_cbk_values             (  TC_CODE             IN    NUMBER,
                                       MERCHANT_DOM          IN   VARCHAR2,
                                       MERCHANT_GROUP     IN    VARCHAR2,
                                       MCC                    IN    VARCHAR2,
                                       COUNTRY                IN  VARCHAR2,
                                       RATE                   OUT  NUMBER,
                                       FIX                    OUT  NUMBER)
  RETURN NUMBER IS
Response       NUMBER:=1;

CURSOR cur_cbk_param IS SELECT CBA_RATE, CBA_FIX
FROM  cbk_prog_assign_rule WHERE
    CBA_TCO = TC_CODE
AND CBA_DOM_CODE = MERCHANT_DOM 
AND CBA_BMG_CODE = MERCHANT_GROUP
AND CBA_MCC_CODE = MCC
AND CBA_MER_COUN_CODE = COUNTRY;

BEGIN
LOOP
FETCH cur_cbk_param into RATE, FIX;
END LOOP;
EXCEPTION
     WHEN NO_DATA_FOUND THEN
Response := 0;
Return(Response);
END;
3
  • First, you need to start with CREATE OR REPLACE FUNCTION, not just FUNCTION. Second, you need to OPEN your cursor before you loop through and fetch the results. Third, you need to EXIT the loop WHEN cur_cbk_param%NOTFOUND. Fourth, a cursor loop like this will not throw a NO_DATA_FOUND exception. Fifth, your function does not RETURN a value unless there is an exception. You must return a value in all situations. Nov 25, 2017 at 13:22
  • To effectively diagnose wha'ts happening, you have to provide the instructions to create the table cbk_pro_assing_rule. Some example data will be helpful.
    – Raul Luna
    Nov 25, 2017 at 13:45
  • Possible duplicate of Oracle ERROR: ORA-00900: invalid SQL statement Nov 25, 2017 at 15:02

2 Answers 2

0

You could use cursor%ROWCOUNT to check if no records are returned instead of no_data_found.Note that you must have a return statement regardless of EXCEPTION. So, you could return 1 for success and 0 for failure.

CREATE OR REPLACE FUNCTION get_cbk_values (
    tc_code          IN NUMBER,
    merchant_dom     IN VARCHAR2,
    merchant_group   IN VARCHAR2,
    mcc              IN VARCHAR2,
    country          IN VARCHAR2,
    rate             OUT NUMBER,
    fix              OUT NUMBER
) RETURN NUMBER IS

    response   NUMBER := 1;
    CURSOR cur_cbk_param IS SELECT
        cba_rate,
        cba_fix
                            FROM
        cbk_prog_assign_rule
                            WHERE
        cba_tco = tc_code
        AND   cba_dom_code = merchant_dom
        AND   cba_bmg_code = merchant_group
        AND   cba_mcc_code = mcc
        AND   cba_mer_coun_code = country;

BEGIN
    OPEN cur_cbk_param;
    LOOP
        FETCH cur_cbk_param INTO rate,fix;
        EXIT WHEN cur_cbk_param%notfound;
    END LOOP;
    IF
        cur_cbk_param%rowcount = 0
    THEN
        response := 0;
    END IF;
    RETURN response;
EXCEPTION
    WHEN OTHERS THEN
--Preferably use dbms_utility.format_error_backtrace or SQLERRM to display or log the message somewhere.
        response := 0;
        RETURN response;
END;
/
0

There are mainly 3 mistakes: 1- Replace Fuction>>Create or Replace Function 2- Just after Begin add this Line>> Open cur_cbk_param ; 3- Add this line in Execution section (Just Before Exception) >>>>Return Response; Note: No need to use Response inside bracket .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.