我试图在两个单独的表中搜索特定值的COUNT
并从函数返回它。
FUNCTION check_parts
(p_partno IN VARCHAR2)
RETURN NUMBER
IS
out_exists NUMBER;
sub_exists NUMBER;
BEGIN
SELECT COUNT(*) INTO out_exists
FROM outline_pn op
WHERE op.outline_pn = p_partno
UNION ALL
SELECT COUNT(*)
FROM sub_pn sp
WHERE sp.sub_assy_pn = p_partno;
-- SELECT (SELECT COUNT(*) AS out_exists
-- FROM outline_pn op
-- WHERE op.outline_pn = p_partno) AS out_exists,
-- (SELECT COUNT(*) AS sub_exists
-- FROM sub_pn sp
-- WHERE sp.sub_assy_pn = p_partno) AS sub_exists
RETURN (out_exists + sub_exists);
END check_parts;
起初,我天真地认为两个COUNT(*)
个问题会起作用......它没有。
如何对两个单独的COUNT
查询的值求和并返回结果?
感谢任何帮助。
答案 0 :(得分:0)
我没有找到不使用两个单独查询的理由。然后你可以返回两者的总和。你可以简单地写下这样的东西:
FUNCTION check_parts
(p_partno IN VARCHAR2)
RETURN NUMBER
IS
out_exists NUMBER;
sub_exists NUMBER;
BEGIN
SELECT COUNT(*) INTO out_exists
FROM outline_pn op
WHERE op.outline_pn = p_partno;
SELECT COUNT(*) INTO sub_exits
FROM sub_pn sp
WHERE sp.sub_assy_pn = p_partno;
RETURN (out_exists + sub_exists);
END check_parts;
答案 1 :(得分:0)
如果你仍想在一个查询中使用它,这里是选项
create FUNCTION check_parts
(p_partno IN VARCHAR2)
RETURN NUMBER
IS
sum_exists NUMBER;
BEGIN
select count(1) into sum_exists from (
SELECT outline_pn
FROM outline_pn op
WHERE op.outline_pn = p_partno
UNION ALL
SELECT sub_assy_pn
FROM sub_pn sp
WHERE sp.sub_assy_pn = p_partno);
RETURN (sum_exists);
END check_parts;