1

I have the following procedure:

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;

I try to run the procedure:

select presadress('Paramount');

But I get the following error message:

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

I suspect that this is because there is some kind of error regarding the in parameters of the procedure, but I have been unable to find a solution.

Tom-db
  • 6,528
  • 3
  • 30
  • 44
Sahand
  • 7,980
  • 23
  • 69
  • 137
  • 2
    You have declared the function to take no arguments. Hence, when you pass an argument, the function has no definition. – Gordon Linoff Sep 21 '15 at 17:39
  • So how do I reformulate it to take in a string of arbitrary length? I thought this was done in the 'DECLARE studioName text;' line. – Sahand Sep 21 '15 at 17:44

2 Answers2

1

Use a function parameter, like @Gordon demonstrates, but you don't need plpgsql for this at all. And the query can simplified (shorter, faster):

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;

Function volatility can be STABLE.

Related:

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
0

I think you want a declaration more like this:

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 Linoff
  • 1,242,037
  • 58
  • 646
  • 786