[英]how to reference a schema variable in plpgsql
我正在尝试学习plpgsql代码以自动执行数据库中的某些数据清理。
我当前的任务是将数字字段中的所有“ 999”值替换为“ NaN”。 我正在尝试做的是:1)找到数字模式中的所有列2)遍历这些列并使用“更新/替换”
我的代码如下。 我认为我的主要问题是找出如何在update语句中引用schema.table(但我确信还有其他事情我做得还不够好)。
我得到的错误是该关系未被识别。 任何援助将不胜感激
贝基
CREATE OR REPLACE FUNCTION household.nodata_replace(schemanm text)
RETURNS VOID as $$
DECLARE
cname text;
tname text;
BEGIN
--FOR col IN
for cname,tname in SELECT column_name::text,table_name::text FROM information_schema.columns
where table_schema = schemanm and data_type in ('integer','double precision')
LOOP
RAISE NOTICE 'cname is: % from %', cname, tname;
EXECUTE 'update '||schemanm::regclass||'.' ||tname::regclass||
' set ' || quote_ident(cname) ||' = replace(' || quote_ident(cname) ||', 999, NaN);';
END LOOP;
END;
$$
LANGUAGE plpgsql;
我宁愿为此使用format()
。 占位符%I
需要时会正确引用标识符。
replace()
用于字符串操作,而不用于替换数字。 要分配值NaN,请使用set xxx = 'NaN'
但是您不能对integer
数值执行此操作。 整数不支持NaN
因此,您的动态SQL归结为:
execute format('update %I.%I set %I = ''NaN'' where %I = 999, schemanm, tname, cname, cname);
但是您将需要更改where子句以不包括integer
列。 您可能还希望包括numeric
和real
:
and data_type in ('numeric','double precision', 'real')
如果您只想标记“缺少信息”,我宁愿在这些列中存储null
。 在这种情况下,您无需区分不同的数据类型:
execute format('update %I.%I set %I = null where %I = 999, schemanm, tname, cname, cname);
尝试重新整理您的查询,如下所示:
EXECUTE 'update '|| (schemanm||'.'||tname)::regclass ||' set ' || quote_ident(cname) ||' = ''NaN'' WHERE '|| quote_ident(cname) ||' = 999;'
因为强制转换为regclass
试图在已注册的关系中进行搜索。 与架构无关。
或者你可以
EXECUTE 'update '|| quote_ident(schemanm)||'.'||quote_ident(tname) ||' set ' || quote_ident(cname) ||' = ''NaN'' WHERE '|| quote_ident(cname) ||' = 999;'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.