Oracle stored procedure: return both result set and out parameters

前端 未结 1 1106
后悔当初
后悔当初 2021-01-21 10:50

Oracle stored procedure has OUT parameter and returns result set, e.g.

create or replace procedure foo(empId IN NUMBER, maxSalary OUT NUMBER) AS BEGIN
    select         


        
相关标签:
1条回答
  • 2021-01-21 11:24

    In Oracle, you cannot run direct select statements without an INTO clause.

    If you are using Oracle 12c and above, you may use a REF CURSOR and DBMS_SQL.RETURN_RESULT

    create or replace procedure foo(empId IN NUMBER, maxSalary OUT NUMBER) AS
    q SYS_REFCURSOR;
     BEGIN
        OPEN q FOR select * from Employee e where e.id >=empId;
         DBMS_SQL.return_result (q); -- This will display the result
        select max(salary) into maxSalary from Employee;
    END;
    

    For previous versions ( 11g,10g) , You could pass a REF CURSOR as an OUT parameter and print it from sqlplus or TOAD by running as script.

    create or replace procedure foo(empId IN NUMBER, maxSalary OUT NUMBER,
       q OUT SYS_REFCURSOR) AS
    
         BEGIN
            OPEN q FOR select * from Employee e where e.id >=empId;
            select max(salary) into maxSalary from Employee;
        END;
    

    Define bind variables before calling the procedure.

    VARIABLE v_empID NUMBER
    VARIABLE v_maxsalary NUMBER
    VARIABLE v_q REFCURSOR
    
    EXEC :v_empID := 101
    EXEC foo(:v_empID,:v_maxsalary,:v_q ) 
    PRINT v_q -- This will display the result from the query.
    
    0 讨论(0)
提交回复
热议问题