开始块中的第二个语句未执行

时间:2017-06-19 13:38:40

标签: oracle stored-procedures plsql

我在Oracle中编写了这个存储过程:

CREATE OR REPLACE FUNCTION GET_SOLVER_ID(username_in IN VARCHAR2) 
RETURN NUMBER 
IS 
solver_id number(19);
system_user_id number(19);
BEGIN 
SELECT id
INTO solver_id
FROM usr_solver
WHERE username = username_in;

select ID into system_user_id from USR_USER where USER_TYPE = 'X';
solver_id := nvl(solver_id, system_user_id);
RETURN(solver_id); 
END;

当我用表usr_solver中不存在的用户名调用该函数时,我得到结果为null。我希望得到system_user_id 似乎开始块中的另一个select语句和nvl函数没有执行 你能帮忙吗,我看不出原因......

谢谢,
mismas

1 个答案:

答案 0 :(得分:2)

这应该做你想要的事情

CREATE OR REPLACE FUNCTION GET_SOLVER_ID(
        username_in IN VARCHAR2)
    RETURN NUMBER
IS
    some_id NUMBER(19);
BEGIN
    BEGIN
        SELECT id
        INTO some_id
        FROM usr_solver
        WHERE username = username_in;
    EXCEPTION
    WHEN NO_DATA_FOUND THEN
        SELECT ID
        INTO some_id
        FROM USR_USER
        WHERE USER_TYPE = 'X';
    END;
    RETURN(some_id);
END;