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

空扰寡人 提交于 2020-08-24 07:01:28

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/25119622/is-there-a-convention-for-named-arguments-in-a-function-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!