Oracle

內置 XML 函式

  • May 20, 2013

我正在嘗試使用內置 XML 函式,但出現%s: invalid identifier錯誤。如果沒有 XML 函式,Selects 可以正常工作。

任何想法錯誤在哪裡?

select id,full_name from users where id in(
with sample_data as
(
(select pm_id 
from pm  where p_id=30369)
)
select cast(t.column_value.extract('//text()') as varchar2(40)) val
 from sample_data,
 table( xmlsequence( xmltype(
   '<x><x>' || replace(pm_id, '#@#', '</x><x>') || '</x></x>'
 ).extract('//x/*'))) t)

@elimerv,

您確定沒有其他東西代替預期的“單引號”嗎?此外,您不知道“pm”表在 pm_id 列中有什麼樣的數據(順便說一句,您的最新 SQL 似乎不正確,因為您的 sample_data 子查詢有一個“id”列,而您的主要查詢是指到“pm_id”列)。這就是我在 11.2.0.1 上得到的首先,您的最新查詢是原樣(使用的表除外):

with sample_data as (
  select '123#@#' id
  from dual
 )
 select sysdate from dual where dummy  in(
   select cast(t.column_value.extract('//text()') as varchar2(400)) val
   from sample_data,
        table(xmlsequence(xmltype('<x><x>'||replace(pm_id,'#@#','</x><x>')||'</x></x>').extract('//x/*'))) t
 )
10  /
        table(xmlsequence(xmltype('<x><x>'||replace(pm_id,'#@#','</x><x>')||'</x></x>').extract('//x/*'))) t
                                                    *
ERROR at line 8:
ORA-00904: "PM_ID": invalid identifier

現在,使用正確的列名:

with sample_data as (
  select '123#@#' pm_id
  from dual
 )
 select sysdate from dual where dummy  in(
   select cast(t.column_value.extract('//text()') as varchar2(400)) val
   from sample_data,
        table(xmlsequence(xmltype('<x><x>'||replace(pm_id,'#@#','</x><x>')||'</x></x>').extract('//x/*'))) t
 )
10  /

no rows selected

而且,當我用不同(但外觀相似)的字元替換其中一個“單引號”時:

with sample_data as (
  select '123#@#' pm_id
  from dual
 )
 select sysdate from dual where dummy  in(
   select cast(t.column_value.extract('//text()') as varchar2(400)) val
   from sample_data,
        table(xmlsequence(xmltype('<x><x>'||replace(pm_id,'#@#','</x><x>')||'</x></x>').extract(`//x/*'))) t
 )
10  /
        table(xmlsequence(xmltype('<x><x>'||replace(pm_id,'#@#','</x><x>')||'</x></x>').extract(`//x/*'))) t
                                                                                                *
ERROR at line 8:
ORA-00911: invalid character

這讓你有什麼想法嗎?順便說一句,您確定需要 XML 處理來解決您的問題嗎?

我認為菲爾是在正確的軌道上。您需要將 CTE(“WITH 子句”)移出子選擇

試試這個:

with sample_data as (
select pm_id 
from pm  
where p_id=30369
)
select id,
      full_name 
from users 
where id in(
 select cast(t.column_value.extract('//text()') as varchar2(40)) val
 from sample_data,
      table(xmlsequence(xmltype('<x><x>'||replace(pm_id,'#@#','</x><x>')||'</x></x>').extract('//x/*'))) t
)

引用自:https://dba.stackexchange.com/questions/41896