如果函数的参数与列名相同怎么办

Pau*_*aul 3 sql database postgresql sql-function

我有一个 SQL 函数,其参数名称为 id。但是,我有一个具有相同名称 id 的列名。我如何告诉函数如何区分参数和列。如果我将参数名称从 id 更改为 num,我的函数可以完美运行,但这不是这里的选项,而且我也无法更改列名称。

create or replace function
    test(id integer) return text
as $$
    select address
    from customers c
    where c.id = id
$$ language sql;
Run Code Online (Sandbox Code Playgroud)

Gor*_*off 5

Postgres 允许您按位置引用参数:

create or replace function
    test(id integer) return text
as $$
    select address
    from customers c
    where c.id = $1
$$ language sql;
Run Code Online (Sandbox Code Playgroud)

我认为这是一种不好的做法,班级不应该鼓励这种风格。事实上,应该鼓励您为不太可能与其他标识符冲突的参数命名:

create or replace function test (
        in_id integer
) return text
as $$
    select address
    from customers c
    where c.id = in_id
$$ language sql;
Run Code Online (Sandbox Code Playgroud)