简体   繁体   English

在DB2脚本中将SQL查询结果导出为XML

[英]Export SQL query results as XML in DB2 script

I'm trying to query a lot of system/database information in an SQL script to be run on DB2 Linux/Unix/Windows. 我试图在要在DB2 Linux / Unix / Windows上运行的SQL脚本中查询很多系统/数据库信息。 Ideally the output should be both machine readable and at least somewhat comprehensible for humans. 理想情况下,输出应该是机器可读的,并且至少对于人类来说是可以理解的。

As formatting the output in DB2 CLP seems erratic (or I'm too newb) I tried to output stuff as XML and process it later on, something like: 由于在DB2 CLP中格式化输出似乎很不稳定(或者我太新了),所以我尝试将内容输出为XML并在以后进行处理,例如:

VALUES ('<?xml version="1.0"?>');
SELECT REC2XML(1.0, 'COLATTVAL', 'row', NAME, VALUE, DEFERRED_VALUE) FROM SYSIBMADM.DBMCFG ORDER BY NAME;

This works ok; 这样行得通; I get output I can probably process further. 我得到的输出可能会进一步处理。

However, when I try to run REC2XML with a function such as current_timestamp: 但是,当我尝试使用诸如current_timestamp之类的功能运行REC2XML时:

SELECT Current_timestamp FROM sysibm.sysdummy1;

I can't use REC2XML as that only seems to support column selects. 我不能使用REC2XML,因为它似乎仅支持列选择。

Not having much DB2 knowledge: 没有太多的DB2知识:

  1. Is there a better/easier way to generate XML output from an SQL script 是否有更好/更简便的方法从SQL脚本生成XML输出
  2. Is there some way to also output the results of functions such as current_timestamp as XML? 有没有办法将诸如current_timestamp之类的函数的结果也输出为XML?

Note that I'm after an SQL script (not a shell script); 请注意,我使用的是SQL脚本(而不是Shell脚本)。 if possible I would like to keep it usable on Windows, Linux and Unix machines... 如果可能的话,我想让它在Windows,Linux和Unix机器上保持可用...

  1. I don't know of a better way. 我不知道有更好的方法。
  2. You can get XML output of functions like this: 您可以获取如下功能的XML输出:

     Select REC2XML(1.0, 'COLATTVAL', 'row', ct) From (SELECT current timestamp as ct FROM sysibm.sysdummy1); 

This function does not exist in DB2 for zOS. 此功能在DB2 for zOS中不存在。 The alternate is to use XMLFOREST to generate a column set and XMLAGG to merge the rows. 替代方法是使用XMLFOREST生成列集,并使用XMLAGG合并行。

SELECT
xmlserialize(
    xmlelement(
        name "employees",
        xmlagg(
            xmlelement(
                name "employee",
                xmlforest(
                    EMPNO as "empno",
                    SURNAME as "surname",
                    MIDNAME as "midname",
                    FIRSTNAME as "firstname",
                    HIREDATE as "hiredate"
                )
            )   
            order by EMPNO
        )
    AS CLOB VERSION '1.0' INCLUDING XMLDECLARATION
    )
)
from EMPLOYEES

Sample: 样品:

<?xml version="1.0" encoding="UTF-8"?><employees><employee><empno>12345</empno> ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM