postgresql - 这是什么“错误:不明确的列引用错误”?
问题描述
我想用表1中的代码在表2中找到的值填充表1的空字段
CREATE OR REPLACE FUNCTION public.add_soate(
)
RETURNS SETOF zagsmarriagelist
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
DECLARE
r zagsmarriagelist%rowtype;
DECLARE code varchar;
BEGIN
FOR r IN
SELECT id FROM zagsmarriagelist
LOOP
code := (select nullif(regexp_replace(r::varchar, '\D','','g'), ''));
UPDATE zagsmarriagelist
SET bridesoate = (select substring(a.code from 1 for 14) from ate_history a where a.ate::varchar=(select bridebirthaddress from zagsmarriagelist where id::varchar=code))
WHERE id::varchar=code;
RETURN NEXT r;
END LOOP;
RETURN;
END
$BODY$;
ALTER FUNCTION public.add_soate()
OWNER TO postgres;
select * from add_soate();
显示错误:
错误:错误:对列“代码”的模糊引用第 2 行:... ess from zagsmarriagelist z where z.id::varchar = code)) as ... ^ 详细信息:假设引用 PL / pgSQL 变量或表柱子。QUERY: UPDATE zagsmarriagelist SET Brisoate = (case when (select z.bridebirthaddress from zagsmarriagelist z where z.id::varchar = code)! = '' Then cast ((select substring (a.code from 1 for 14) from ate_history a where a.ate :: varchar = (select z.bridebirthaddress from zagsmarriagelist z where z.id::varchar = code)) as integer) else NULL END),groomsoate = (case when (select z.groombirthaddress from zagsmarriagelist z where z .id::varchar = code)! = '' 然后从 ate_history a where a.ate: 中强制转换 ((select substring (a.code from 1 for 14) where a.ate: : varchar = (select z.bridebirthaddress from zagsmarriagelist z where z.id ::
为什么它不能识别子查询中的变量“代码”?
解决方案
问题在这里:
where id::varchar = code
因为ate_history
有一个名为的列code
并且您定义了一个 variable code
,所以对于表达式所指的那个是模棱两可的,因为两者都在范围内。
通常你只需要限定它,但你不能只重命名它。
declare _code
_code := (select nullif(regexp_replace(r::varchar, '\D','','g'), ''));
where id::varchar = _code
或者,如果您想要以下列ate_history
:
where id::varchar = a.code
推荐阅读
- javascript - 如何将异步等待应用于我的功能 agregarEstadoLike()
- lua - (lua) 这个搜索函数中参数t的作用是什么?
- c - 在 C 项目上运行 Splint | 包括问题并在一个完整的项目上运行
- java - 可以使用 MethodHandle 常量绕过访问控制吗?
- c++ - 有没有办法简化 x-drive 中的重复添加?
- amazon-web-services - 如何使用 AWS CDK 从 EC2 Autoscaling Group 获取 EC2 ID 和私有 IP
- sqlite - 为其他列的每个不同值选择列的所有最大值
- python - 如何绘制 -log(x)?
- python - 为什么我在 Python Pandas dtype float64 上出现错误?
- ssl-certificate - 配置 SSL 证书时产生 vsftpd 服务器错误的原因是什么?