PostgreSQL 中的函数中是否有命名参数的约定 [英] Is there a convention for named arguments in a function in PostgreSQL
问题描述
我来自 SQL Server 背景,其中在存储过程中使用/鼓励使用@"符号.这很有用,因为您可以轻松查看什么是列以及什么是值.例如.
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
我知道我只能使用序数位置,但我们存储的一些过程有 20 个左右的参数,并且命名参数使其在 IMO 中更加清晰易读.
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.
PostgreSQL 社区是否有某种约定用于前缀?我试图找出命名参数的确切规则,但我的谷歌搜索没有产生任何结果.
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.
推荐答案
参数标识符与其他标识符遵循相同的规则:
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
这篇关于PostgreSQL 中的函数中是否有命名参数的约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!