Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a convention for named arguments in a function in PostgreSQL

I come from a SQL Server background where the '@' symbol is used/encouraged in stored procedures. This is useful because you can easily see what is a column and what is a value. For example.

CREATE PROCEDURE Foo
    @Bar    VARCHAR(10),
    @Baz    INT
AS
BEGIN
    INSERT INTO MyTable (
        Bar,
        Baz)
    VALUES (
        @Bar,
        @Baz)

END

I know that I can just use ordinal position but some of our stored procs have 20 or so parameters and the named parameter makes it much more legible IMO.

Is there some sort of convention that the PostgreSQL communitiy uses for a prefix? I tried to find out exactly what the rules were for named parameters but my Googling didn't yield anything.

like image 626
uriDium Avatar asked Aug 04 '14 13:08

uriDium


1 Answers

Parameter identifiers follow the same rules as other identifiers:

http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

http://www.postgresql.org/docs/current/static/xfunc-sql.html#XFUNC-SQL-FUNCTION-ARGUMENTS

It is common to start a parameter identifier with an underscore _ and I think it makes sense although it is not a convention.

It is also possible to avoid ambiguity by qualifying the identifier with the function name

my_funtion.my_parameter
like image 199
Clodoaldo Neto Avatar answered Nov 16 '22 04:11

Clodoaldo Neto