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

发布于 2024-12-12 02:59:05 字数 754 浏览 0 评论 0原文

我正在尝试在要在 DB2 Linux/Unix/Windows 上运行的 SQL 脚本中查询大量系统/数据库信息。 理想情况下,输出应该是机器可读的,并且至少在某种程度上是人类可以理解的。

由于在 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 work ok;我得到的输出可能可以进一步处理。

但是,当我尝试使用 current_timestamp: 等函数运行 REC2XML 时,

SELECT Current_timestamp FROM sysibm.sysdummy1;

我无法使用 REC2XML,因为它似乎只支持列选择。

没有太多的 DB2 知识:

  1. 是否有更好/更简单的方法从 SQL 脚本生成 XML 输出
  2. 是否有某种方法也可以将 current_timestamp 等函数的结果输出为 XML?

请注意,我正在寻找 SQL 脚本(不是 shell 脚本);如果可能的话我想让它在 Windows、Linux 和 Unix 机器上可用......

I'm trying to query a lot of system/database information in an SQL script to be run on DB2 Linux/Unix/Windows.
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:

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:

SELECT Current_timestamp FROM sysibm.sysdummy1;

I can't use REC2XML as that only seems to support column selects.

Not having much DB2 knowledge:

  1. Is there a better/easier way to generate XML output from an SQL script
  2. Is there some way to also output the results of functions such as current_timestamp as XML?

Note that I'm after an SQL script (not a shell script); if possible I would like to keep it usable on Windows, Linux and Unix machines...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

汹涌人海 2024-12-19 02:59:05
  1. 我不知道有更好的方法。
  2. 您可以获得如下函数的 XML 输出:

    Select REC2XML(1.0, 'COLATTVAL', 'row', ct) From (SELECT 当前时间戳作为 ct FROM sysibm.sysdummy1);
    
  1. I don't know of a better way.
  2. You can get XML output of functions like this:

    Select REC2XML(1.0, 'COLATTVAL', 'row', ct) From (SELECT current timestamp as ct FROM sysibm.sysdummy1);
    
挖鼻大婶 2024-12-19 02:59:05

DB2 for zOS 中不存在此函数。另一种方法是使用 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

样本:

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

This function does not exist in DB2 for zOS. The alternate is to use XMLFOREST to generate a column set and XMLAGG to merge the rows.

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> ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文