2011-02-03 80 views
0

我有一個SQL語句,像這樣的:Postgres的:檢查從XPath返回()

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

這個問題是關於特定XPath引擎調用的參數數據類型。重新標記。 – 2011-02-07 16:13:16

回答

0

你想:

select coalesce(nullif(xpath('//consumer/contact_address/\@postcode', xml),'{}'::xml[]), '') 
    from consumer 
where docid = 12345; 

注投給XML數組,但遺憾的是沒有對XML []上的平等權利運營商。所以你必須這樣做:

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字符,什麼是機器人SQL符合但可能。使用E'Next \ n行'或在postgresql.conf中禁用此檢查!

+0

我做了相反的事情,我用`select'從消費者處選擇合併(nullif(xpath('// consumer/contact_address/\ @ postcode',xml):: text,'{}'),'',其中docid = 12345; `它暫時有效。謝謝! :) – 2011-02-04 02:50:05