Postgres noobie here.
I\'m trying to convert a SQL Server stored proc into a Postgres function. Currently cannot figure out how to turn this SQL line into Postgres.
REVISED: As pointed out in comments, this answer was accurate when written in early 2012, but named parameters have been supported since v9.2, released late 2012.
Parameter names are merely decoration when your function is in language SQL. You can use the parameters by name in stored procedures defined as language plpgsql
.
Consequently, you must refer to the function args using $X where X is the ordinal position of the function's argument list (starting with 1).
CREATE OR REPLACE FUNCTION fn_name (
n VARCHAR(32) = NULL,
OUT name varchar(32),
OUT description varchar(64) )
RETURNS setof record
AS
$$
SELECT u.name
, u.description
FROM table_a u
WHERE u.name = COALESCE($1, u.name);
$$
LANGUAGE sql;