CREATE OR REPLACE FUNCTION GetProduct(_product_id NUMERIC,
_product_name OUT products.product_name%TYPE,
_category OUT products.category%TYPE,
_quantity OUT products.quantity%TYPE,
_price OUT products.price%TYPE)
RETURNS VARCHAR
Language 'plpgsql'
AS
$$
DECLARE flag BOOLEAN;
BEGIN
SELECT product_id,product_name,category,quantity,price
INTO _product_id,_product_name,category,quantity,price FROM products
WHERE product_id=_product_id;
flag=true;
RETURN flag;
END;
$$
我试过这种方法,但它显示出一些错误,比如
NOTICE: type reference products.price%TYPE converted to numeric
ERROR: function result type must be record because of OUT parameters
SQL state: 42P13
注意到这可能是因为您使用的是域,而这被简化为基本类型。
错误表明OUT
参数和声明的函数返回类型相互矛盾:函数不返回单个varchar
,而是返回由几个元素组成的复合值。
因此,遵循错误消息并将函数声明为RETURNS record
。