我有一个类似的SQL语句:
select coalesce(nullif(xpath('//consumer/contact_address/\@postcode', xml),'{}'), '') from consumer where docid = 12345;
它给了我一个错误:
ERROR: array value must start with "{" or dimension information
这甚至意味着什么?此外,如果我使用psql交互式提示尝试它,我会得到另一个神秘的错误:
#psql: select nullif(xpath('//consumer/contact_address/\@street', xml), '{}') from consumer where docid = 12345;
WARNING: nonstandard use of escape in a string literal
LINE 1: select nullif(xpath('//consumer/contact_address/\@street', x...
^
HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
ERROR: could not identify an equality operator for type xml
这里有什么问题?谢谢!
答案 0 :(得分:0)
你想:
select coalesce(nullif(xpath('//consumer/contact_address/\@postcode', xml),'{}'::xml[]), '')
from consumer
where docid = 12345;
注意转换为xml数组,但不幸的是xml [] s没有相等运算符。所以你必须这样做:
select CASE WHEN array_upper(xpath('//consumer/contact_address/\@postcode', xml)) = 1
THEN xpath('//consumer/contact_address/\@postcode', xml) END
from consumer
where docid = 12345;
未经测试,玩上限,也许你必须再次检查零。不用担心,PostgreSQL会检测到类似的子表单并仅评估xpath表达式一次。
警告导致在字符串文字中使用Escape字符,什么是bot SQL符合,但是可行。使用E'Next \ n line'或在postgresql.conf中禁用此检查!