postgresql - 函数空值错误

时间:2015-09-24 20:41:16

标签: postgresql

我试图在尝试选择我的功能时弄清楚如何解决此ERROR: null values cannot be formatted as an SQL identifier

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_%'"

它使用的基本查询不会产生null values。那么它来自哪里?

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;

这是我的创建声明:

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;

0 个答案:

没有答案