Sql 如何将数据从表插入XML表

Sql 如何将数据从表插入XML表,sql,xml,oracle,cursor,computer-science,Sql,Xml,Oracle,Cursor,Computer Science,当我运行这段代码时,我没有从标准表中获得正确的数据 create table COUNTRY_XML( C_X sys.xmltype); begin for cursor in (select country, continental, population2019 from countries) loop insert into country_xml values( sys.xmltype.createXML( '<ac_x

当我运行这段代码时,我没有从标准表中获得正确的数据

create table COUNTRY_XML(
C_X sys.xmltype);

begin 
    for cursor in
       (select country, continental, population2019 from countries)
    loop
    insert into country_xml values(
    sys.xmltype.createXML(
    '<ac_x createdby="Guangzhe">
       <country_info>
       <Country>mycursor.country</Country>
       <Continental>mycursor.continental</Continental>
       <Population2019>mycursor.population2019</Population2019>
       </country_info>
    </ac_x>'));
    end loop;
end;

select c.c_x.extract('/').getstringval() from country_xml c
结果如下,每一行的结果都是一样的

"<ac_x createdby="Guangzhe">
  <country_info>
    <Country>mycursor.country</Country>
    <Continental>mycursor.continental</Continental>
    <Population2019>mycursor.population2019</Population2019>
  </country_info>
</ac_x>"
你做得不好

您需要使用| | concatation操作符,如下所示

表创建

您需要的解决方案:

输出


干杯

问题是,我想向印度这样的国家展示xml格式,对我的游标值使用串联。我猜你指的是光标而不是我的光标。谢谢你,先生!你说得对!顺便问一下,我可以再问一个问题吗?我想计算每个大陆的人口总数,并使用字母顺序对其进行排序,因此我的代码如下:选择c.c_x.extract'/country_info/continental'.getstringval,sumc.c_x.extract'/country_info/population2019'.getstringval由c.c_x.extract'/country_info/continental'由c.c_x.extract'/country_info/continental'命令由c.c_x.extract'/country_info/continental'命令由c.c.c_x.命令,但输出表示没有MAP或order方法无法对对象进行排序,请告诉我哪里错了您必须看到这个答案:
SQL> CREATE TABLE COUNTRY_XML (
  2      C_X   SYS.XMLTYPE
  3  );

Table created.
SQL>
SQL> begin
  2      for mycursor in -- changed the name
  3         (select 'INDIA' as country, 'ASIA' as continental, '130B' as population2019 from dual)
  4         --(select country, continental, population2019 from countries)
  5      loop
  6      insert into country_xml values(
  7      sys.xmltype.createXML(
  8      '<ac_x createdby="Guangzhe">
  9         <country_info>
 10         <Country>' || mycursor.country || '</Country>
 11         <Continental>' || mycursor.continental|| '</Continental>
 12         <Population2019>' || mycursor.population2019|| '</Population2019>
 13         </country_info>
 14      </ac_x>'));
 15      end loop;
 16  end;
 17  /

PL/SQL procedure successfully completed.
SQL> SELECT C.C_X.EXTRACT('/').GETSTRINGVAL()
  2  FROM COUNTRY_XML C
  3  ;

C.C_X.EXTRACT('/').GETSTRINGVAL()
--------------------------------------------------------------------------------
<ac_x createdby="Guangzhe">
  <country_info>
    <Country>INDIA</Country>
    <Continental>ASIA</Continental>
    <Population2019>130B</Population2019>
  </country_info>
</ac_x>


SQL>