当我调用以下PL / SQL函数时,出现错误“找不到数据”
FUNCTION get_Deployment_Status(i_deploymentId deployments.pk%type)
RETURN VARCHAR2
IS
o_status varchar2(30);
BEGIN
SELECT distinct Status
into o_status
FROM deployments
WHERE Pk=i_deploymentId;
return o_status;
END;
我传递了Deploymentid(这是一个数字),我希望状态以字符串形式返回。有更好的方法吗?为什么我会收到该错误?
答案 0 :(得分:2)
看看以下选项。阅读代码中的注释。
选项1:
FUNCTION get_deployment_status (i_deploymentid deployments.pk%TYPE)
RETURN VARCHAR2
IS
o_status VARCHAR2 (30);
BEGIN
-- This option will return O_STATUS if something is found. If not, NO_DATA_FOUND
-- EXCEPTION section will handle it.
-- You most probably don't need DISTINCT nor TOO_MANY_ROWS handling as it appears that
-- you're dealing with a primary key column; are you? If not, well, you'll have to
-- handle that option as well.
SELECT status
INTO o_status
FROM deployments
WHERE pk = i_deploymentid;
RETURN o_status;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
RETURN NULL;
END;
选项2:
FUNCTION get_deployment_status (i_deploymentid deployments.pk%TYPE)
RETURN VARCHAR2
IS
o_status VARCHAR2 (30);
BEGIN
-- This option will return NULL into O_STATUS if there's nothing found.
-- It won't raise the NO_DATA_FOUND exception.
-- Just like above, if it is a primary key column involved in the WHERE
-- clause, you shouldn't care whether MAX will return correct value or not,
-- because - if it exists - it will be the only value.
SELECT MAX (status)
INTO o_status
FROM deployments
WHERE pk = i_deploymentid;
RETURN o_status;
END;