postgresql-函数上出现空值错误
我正在尝试解决此postgresql-函数上出现空值错误,postgresql,Postgresql,我正在尝试解决此错误:当尝试选择我的函数时,空值不能格式化为SQL标识符: select * from store_keys(); ERROR: null values cannot be formatted as an SQL identifier CONTEXT: SQL statement "SELECT string_agg( format('SELECT %1$I, count(email_store_key), email_store_key, form_created_
错误:当尝试选择我的函数时,空值不能格式化为SQL标识符
:
select * from store_keys();
ERROR: null values cannot be formatted as an SQL identifier
CONTEXT: SQL statement "SELECT string_agg(
format('SELECT %1$I, count(email_store_key), email_store_key, form_created_datetime
FROM %1$I where email_store_key=0 GROUP BY 3, 4', tbl_name),
' UNION ') FROM information_schema.tables
WHERE table_schema = 'abc_dev_sch_1234'
AND table_name LIKE 'fact_%'"
它使用的基本查询不会产生空值
。那么这是从哪里来的
select count(email_store_key), email_store_key, form_created_datetime
FROM <table_name> where email_store_key=0 GROUP BY email_store_key, form_created_datetime;
我想是简单的打字错误。您的
format
调用使用的是输出变量tbl\u name
,而不是列table\u name
。顺便说一句,我认为您的第一个格式说明符需要是%1$L
,而不是%1$I
@NickBarnes在此上下文中,%I和%L之间唯一显著的区别是前者在必要时是双引号;两者都产生一个字符串值。该错误是由于%I不接受从未初始化的“tbl_name”输出参数传入的空值。确实是简单的打字。@Patrick:%L
生成一个带引号的值,如quote\u literal()
DROP FUNCTION store_keys();
CREATE OR REPLACE FUNCTION store_keys()
RETURNS TABLE (tbl_name varchar, count_keys bigint, email_store_key integer, form_created_datetime timestamp)
AS $$
DECLARE
qry text;
BEGIN
SELECT string_agg(
format('SELECT %1$I, count(email_store_key), email_store_key, form_created_datetime
FROM %1$I where email_store_key=0 GROUP BY 3, 4', tbl_name),
' UNION ') INTO qry
FROM information_schema.tables
WHERE table_schema = 'abc_dev_sch_1234'
AND table_name LIKE 'fact_%';
RETURN QUERY EXECUTE qry;
END
$$ LANGUAGE plpgsql;