sql - 如果函数的参数与列名相同怎么办
问题描述
我有一个 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;
解决方案
Postgres 允许您按位置引用参数:
create or replace function
test(id integer) return text
as $$
select address
from customers c
where c.id = $1
$$ language sql;
我认为这是一种不好的做法,一个班级不应该鼓励这种风格。实际上,应该鼓励您为不太可能与其他标识符冲突的参数命名:
create or replace function test (
in_id integer
) return text
as $$
select address
from customers c
where c.id = in_id
$$ language sql;
推荐阅读
- google-apps-script - 通过 https://groups.google.com/ cf AdminDirectory.Members.insert 添加时向群组成员发送邀请邮件
- python - 在我的距离计算器中不明白 TypeError 的原因
- facebook-graph-api - Facebook 登录无法请求 instagram_basic 权限
- xcode - 您知道我没有收到 Console.log 或 Appstoreconnect 崩溃报告的原因吗?
- java - Spring Boot - @MockBean 在测试用例中创建重复项
- vba - VBA 代码在逐步模式下工作,但在尝试运行宏时崩溃
- python - TypeError:不允许捕获不继承自 BaseException 的类
- javascript - 根据当前页面 url 更改按钮 href
- prometheus - 在开发过程中如何查询prometheus并控制结果?
- python-3.x - 如何附加包含在嵌套列表中的多个字典值