【问题标题】:I can't extract data using fn:collection我无法使用 fn:collection 提取数据
【发布时间】:2023-03-28 13:39:01
【问题描述】:

我正在使用 Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production

我有现有的表,例如:my_scheme.my_existing_table 我可以选择并查看此表中的数据。

但是当我尝试使用 XMLQuery 从该表中读取数据时:

SELECT XMLQuery(
 'for $i in fn:collection("oradb:/my_scheme/my_existing_table")/content/text()
 return $i'
 returning content
)FROM DUAL;

Oracle 生成错误:

ORA-00942: table or view does not exist

也许有人缺少某些权利。请告诉我如何解决这个问题。

【问题讨论】:

  • 不清楚,因为您更改了名称,但您使用的是my_scheme/my_existing_table 还是MY_SCHEME/MY_EXISTING_TABLEThe case matters;也不确定 XPath 的 /content/text() 部分正在尝试做什么。

标签: oracle collections qxmlquery


【解决方案1】:

例如,你有一个表T

SQL> create table t as select level a, 'b' b from dual connect by level<=10;

Table created.

SQL> select * from t;

         A B
---------- -
         1 b
         2 b
         3 b
         4 b
         5 b
         6 b
         7 b
         8 b
         9 b
        10 b

10 rows selected.

您的查询应该是这样的:


SELECT XMLQuery(
 'for $i in fn:collection("oradb:/XTENDER/T")/ROW/A/text()
 return $i'
 returning content
) as res
FROM DUAL;

RES
----------------------------
12345678910

fn:collection()参数应该是"oradb:/SCHEMA/TABNAME",然后你应该指定/ROW/COLNAME,其中/ROW在列名之前是强制性的。

事实上,如果这真的是你想要的,你就不需要for

SELECT XMLQuery('fn:collection("oradb:/XTENDER/T")/ROW/A/text()' returning content) res FROM DUAL;

虽然我会将这些值与, 连接起来,如下所示:

SELECT
   XMLQuery('
     fn:string-join(
       fn:collection("oradb:/XTENDER/T")/ROW/A/text()
       ,","
       )
    ' returning content) as res 
FROM DUAL;

RES
--------------------------------------------------
1,2,3,4,5,6,7,8,9,10

或者 T 中所有列的几个变体:

SELECT
   XMLQuery('
     fn:string-join(
       fn:collection("oradb:/XTENDER/T")/ROW/*
       ,","
       )
    ' returning content) as res 
FROM DUAL;

RES
--------------------------------------------------
1,b,2,b,3,b,4,b,5,b,6,b,7,b,8,b,9,b,10,b


SELECT
   XMLQuery('
     fn:string-join(
       for $i in fn:collection("oradb:/XTENDER/T")/ROW return string-join($i/*, ",")
       ,"; "
       )
    ' returning content) as res 
FROM DUAL;

RES
--------------------------------------------------
1,b; 2,b; 3,b; 4,b; 5,b; 6,b; 7,b; 8,b; 9,b; 10,b

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2020-06-24
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多