PostgreSQL:没有函数匹配给定的名称和参数类型

PostgreSQL: No function matches the given name and argument types

我有以下程序:

DROP FUNCTION presAdress();

CREATE FUNCTION presadress() RETURNS VARCHAR(100) AS $$
DECLARE studioName text;
    BEGIN

    RETURN (
    SELECT address AS pres_address
    FROM MovieExec
    WHERE cert# IN (
        SELECT presC# 
        FROM Studio
        WHERE name = studioName)
        );
    END;

$$ LANGUAGE plpgsql;

我尝试运行程序:

select presadress('Paramount');

但我收到以下错误消息:

ERROR: function presadress(text) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 294

我怀疑这是因为程序的输入参数有某种错误,但我一直找不到解决办法。

我想你更想要这样的声明:

CREATE FUNCTION presadress (v_studioName text)
    RETURNS VARCHAR(100) AS $$
BEGIN
    RETURN(SELECT address AS pres_address
           FROM MovieExec
           WHERE cert# IN (SELECT presC# 
                           FROM Studio
                           WHERE name = v_studioName)
         );
END;
$$ LANGUAGE plpgsql;

使用函数参数,就像@Gordon 演示的那样,但您根本不需要 plpgsql。并且查询可以简化(更短,更快):

CREATE FUNCTION presadress(_studioname text)
  RETURNS text AS
$$
SELECT m.address
FROM   studio s
JOIN   movieexec m ON m.cert# = s.presc#
WHERE  s.name = _studioname
$$ LANGUAGE sql STABLE;

函数波动可以是STABLE.

相关: