我想用表2中表1中的代码找到的值填充表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 来自zagsmarriagelist z,其中z.id::varchar =代码))为... ^详细信息: 假定对PL / pgSQL变量或表列的引用。查询: UPDATE zagsmarriagelist SET Bridesoate =(case when(select zagsmarriagelist z中的z.bridebirth地址,其中z.id::varchar = 码)! =''然后强制转换((选择子字符串(从1到14的代码) ate_history a其中a.ate :: varchar =(选择z.bridebirthaddress 来自zagsmarriagelist z,其中z.id::varchar = code))为整数)else NULL END),修饰符=(当从(选择z.groombirthaddress zagsmarriagelist z,其中z.id::varchar =代码)! =''然后投 ((从ate_history中选择子字符串(从1到14的代码) a.ate :: varchar =(从zagsmarriagelist z中选择z.bridebirthaddress 其中z.id::varchar = code))为整数),否则为NULL END)WHERE zagsmarriagelist.id::varchar=code语境:PL / pgSQL add_soate() 函数,第13行,语句SQL语句
为什么它不能在子查询中识别变量'code'?
答案 0 :(得分:1)
问题在这里:
where id::varchar = code
因为ate_history
有一个名为code
的列并且您定义了一个变量code
,所以表达式所指的是哪个模棱两可在范围内。
通常,您只需要对它进行限定即可,但是不能仅对它进行唯一重命名。
declare _code
_code := (select nullif(regexp_replace(r::varchar, '\D','','g'), ''));
where id::varchar = _code
或者如果您想从ate_history
中访问该列:
where id::varchar = a.code