我正在尝试在PostgreSQL中创建一个存储过程。该功能在我使用float
时有效,但当我尝试将float
更改为numeric
时出现错误:
ERROR: cannot change return type of existing function
SQL state: 42P13
Detail: Row type defined by OUT parameters is different.
Hint: Use DROP FUNCTION percent_grades(text) first.
我真的不知道这意味着什么,但我认为我不需要做CAST
以外的任何事情来更改type
。
我的功能是:
CREATE OR REPLACE FUNCTION percent_grades(text)
RETURNS TABLE(grade text, percent float)
AS
$$
DECLARE percent_total float;
BEGIN
RETURN QUERY
SELECT psa_grade_name,
SUM(psa_amount) / CAST(total_cards($1) AS float) AS percent_total
FROM psa_pop_view
WHERE psa_card = $1
GROUP BY psa_grade_name
ORDER BY psa_grade_name;
END;
$$
LANGUAGE plpgsql;
我想用float
替换numeric(5, 4)
。
答案 0 :(得分:5)
create or replace
语句无法更改函数的签名。如错误所示,您必须首先删除该功能:
DROP FUNCTION percent_grades(text);
然后才用正确的签名重新创建它:
CREATE OR REPLACE FUNCTION percent_grades(text)
RETURNS TABLE(grade text, percent numeric(5,4))
AS
$$
DECLARE percent_total float;
BEGIN
RETURN QUERY
SELECT psa_grade_name,
SUM(psa_amount) / CAST(total_cards($1) AS float) AS percent_total
FROM psa_pop_view
WHERE psa_card = $1
GROUP BY psa_grade_name
ORDER BY psa_grade_name;
END;
$$
LANGUAGE plpgsql;