我有下一个PL/SQL
代码:
create or replace FUNCTION NUMBER_PLATES (name VARCHAR2)
RETURN INT
IS
num_plates INT;
BEGIN
SELECT count(*) INTO num_plates
FROM plate p, detail_ped dt
WHERE dt.plate = p.cod_plate AND p.name = name;
RETURN (num_plates);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('ERROR');
END NUMBER_PLATES;
下一步是将函数执行到SQL命令中:
DECLARE a INT;
BEGIN
a := NUMBER_PLATES('chicken');
DBMS_OUTPUT.PUT_LINE(a);
END;
但是当函数真正为3时,该函数返回0。
我在做什么错了?
如果我执行我的SQL
句子,则会返回3:
SELECT count(*)
FROM plate p, detail_ped dt
WHERE dt.plate = p.cod_plate AND p.name = 'chicken';
答案 0 :(得分:1)
您应该重命名参数名称,以避免名称冲突:
create or replace FUNCTION NUMBER_PLATES (p_name VARCHAR2)
RETURN INT
IS
num_plates INT;
BEGIN
SELECT count(*) INTO num_plates
FROM plate p
JOIN detail_ped dt -- proper JOIN syntax
ON dt.plate = p.cod_plate
WHERE p.name = p_name;
RETURN (num_plates);
-- exception is dead code, COUNT(*) will return 0 if no records found
-- EXCEPTION
-- WHEN NO_DATA_FOUND THEN
-- DBMS_OUTPUT.PUT_LINE('ERROR');
END NUMBER_PLATES;
/