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

What do I do if parameter of function is the same as a column name

我有一个 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;

如果您这样表达查询是否有效?

select address
from (select c.id as cid, c.address from customers c) t
where cid = id

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

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

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

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