Why we can't execute stored procedure in select statement in oracle? is there any strong reason?

后端 未结 3 922
情书的邮戳
情书的邮戳 2021-01-07 14:00
create or replace procedure pr
is
    v_date date; 
begin
    select sysdate into v_date from dual;
    DBMS_output.put_line(v_date);
end pr;
3条回答
  •  自闭症患者
    2021-01-07 14:30

    Procedure cannot be executed using select statement, you can use function if you would want to execute using select statement.

    If you would want to execute procedure using select statement then one approach is wrap your procedure with a function and call function using select statement.

    CREATE OR REPLACE PROCEDURE pr (o_param OUT DATE)
    IS
        v_date  DATE;
    BEGIN
        SELECT  SYSDATE
          INTO  v_date
          FROM  DUAL;
    
        o_param := v_date;
    END pr;
    

    Now wrap the procedure with a function

    CREATE OR REPLACE FUNCTION my_funct
        RETURN DATE
    AS
        o_param  DATE;
    BEGIN
        pr (o_param);
    
        RETURN o_param;
    EXCEPTION
        WHEN NO_DATA_FOUND
        THEN
            DBMS_OUTPUT.put_line (
            DBMS_UTILITY.format_error_backtrace || ' ' || SQLERRM
        );
                    -- raise exception 
        WHEN OTHERS
        THEN
            DBMS_OUTPUT.put_line (
            DBMS_UTILITY.format_error_backtrace || ' ' || SQLERRM
        );
                    -- raise exception  
    END my_funct;
    /
    

    And call the function using select statement

    SELECT my_funct  FROM DUAL
    

提交回复
热议问题