简体   繁体   English

将 SQL output 从 IBM i Db2 写入 Z3501BB093D363810BZ71058 格式的 IFS 到 IFS。

[英]Write SQL output from IBM i Db2 to IFS in XML format using QSYS2.IFS_WRITE

I am attempting to write XML output from a Db2 file to the IFS.我正在尝试将 XML output 从 Db2 文件写入 IFS。

Running this SQL statement from System i Navigator SQL gives me the proper output on the navigator window:从 System i Navigator SQL 运行此 SQL 语句给了我正确的 output

select xmlelement (name "root",  
    xmlelement (name "EmployeeList",
      XMLAGG (
        XMLELEMENT (NAME "Employee",
          XMLFOREST (
            trim(id) as "ID",
            trim(firstname) as "FirstName",
            trim(lastname) as "LastName"
        )
      )
    )
  )
)
FROM myfile;

Below line feeds and spacing added for clarity:为清楚起见,添加了以下换行符和间距:

<root>
 <EmployeeList>
  <Employee>
   <ID>1</ID>
   <FirstName>ROBERT</FirstName>
   <LastName>JONES</LastName>
  </Employee>
  <Employee>
   <ID>2</ID>
   <FirstName>SMITH</FirstName>
   <LastName>FRED</LastName>
  </Employee>
 </EmployeeList>
</root>

I have no problem writing to the IFS if I use constants like this:如果我使用这样的常量,我可以毫无问题地写入 IFS:

CALL QSYS2.IFS_WRITE(
  PATH_NAME =>'/myFileInXML.xml',
  OVERWRITE => 'REPLACE',
  FILE_CCSID => 1208,
  LINE => ('<root><EmployeeList><Employee><ID>99</ID><FirstName>Joe</FirstName><LastName>Arbuckle</LastName></Employee></EmployeeList></root>'
 )
);

However, if I combine the select with the IFS write like this, I get an error my argument is not valid:但是,如果我像这样将 select 与 IFS 写入结合起来,我会收到一个错误,我的参数无效:

CALL QSYS2.IFS_WRITE(
  PATH_NAME =>'/myFileInXML2.xml',
  OVERWRITE => 'REPLACE',
  FILE_CCSID => 1208,
  LINE => (

select xmlelement (name "root",  
    xmlelement (name "EmployeeList",
      XMLAGG (
        XMLELEMENT (NAME "Employee",
          XMLFOREST (
            trim(id) as "ID",
            trim(firstname) as "FirstName",
            trim(lastname) as "LastName"
        )
      )
    )
  )
)
FROM myfile

 )
);

SQL State: 07006 Vendor Code: -301 Message: [SQL0301] Input variable *N or argument 4 not valid. SQL State:07006 供应商代码:-301 消息:[SQL0301] 输入变量 *N 或参数 4 无效。 Cause.原因。 . . . . . . . . : The value in relative position 4 in the statement is a type that is not compatible with the requested operation. : 语句中相对 position 4 中的值是与请求的操作不兼容的类型。 The value is variable *N, entry 4 in a descriptor area, or argument *N in a CALL statement.该值是变量 *N、描述符区域中的条目 4 或 CALL 语句中的参数 *N。 A name *N indicates that a user's descriptor area was used or that a constant or special register was specified on the CALL statement.名称 *N 表示使用了用户的描述符区域,或者在 CALL 语句中指定了常量或特殊寄存器。 Recovery.恢复。 . . . . : Do one of the following and try the request again: -- Use a variable that is the correct type. :执行以下操作之一并再次尝试请求: -- 使用正确类型的变量。 -- Specify an argument in the CALL that is the correct type. -- 在 CALL 中指定一个正确类型的参数。 -- Change the type specified for parameter 4 in the DECLARE PROCEDURE statement. -- 更改在 DECLARE PROCEDURE 语句中为参数 4 指定的类型。

Advice would be appreciated on how to create a stream file on IFS from Db2 output in XML format.关于如何从 Db2 output 在 IFS 上创建 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 文件的建议将不胜感激

Edit - also tried this technique:编辑 - 也尝试了这种技术:

create table myfilexml (info xml);

insert into myfilexml
select xmlelement (name "root",  
    xmlelement (name "EmployeeList",
      XMLAGG (
        XMLELEMENT (NAME "Employee",
          XMLFOREST (
            trim(id) as "ID",
            trim(firstname) as "FirstName",
            trim(lastname) as "LastName"
        )
      )
    )
  )
)
FROM myfile;

Problem here it it only works if there is only one record in the file.问题在这里它只有在文件中只有一条记录时才有效。 The error here is:这里的错误是:

SQL State: 2200L Vendor Code: -20345 Message: [SQ20345] XML value not a well-formed document. SQL State:2200L 供应商代码:-20345 消息:[SQ20345] XML 值未记录良好。 Cause.原因。 . . . . . . . . : The XML value is not a well-formed document. : XML 值不是格式正确的文档。 An XML value that is being stored in a table must be a well-formed XML document with a single root element.存储在表中的 XML 值必须是具有单个根元素的格式良好的 XML 文档。 Recovery.恢复。 . . . . : Change the XML value to be a well-formed document with a single root element. :将 XML 值更改为具有单个根元素的格式良好的文档。 Try the request again.再次尝试请求。

use the xmlserialize function to convert the XMLELEMENT to a CLOB .使用xmlserialize function 将XMLELEMENT转换为CLOB

Here is a query which writes the contents of a source member to a stream file in XML format:这是一个查询,它将源成员的内容写入 XML 格式的 stream 文件:

call  qsys2.ifs_write( path_name=>'/home/steve/xml.txt',    
   overwrite=>'REPLACE', file_ccsid=>1208, line=>  (           
select xmlserialize(xmlelement (name "root",                   
    xmlelement (name "srcmbr",                                 
      XMLAGG (                                                 
        XMLELEMENT (NAME "srcline",                            
          XMLFOREST (                                          
            trim(srcseq) as "srcseq",                          
            trim(srcdat) as "srcdat",                          
            trim(srcdta) as "srcdta"                           
        ))))                                                   
)  as clob(50k)) srccode                                       
FROM qrpglesrc    ))                                           

You need to write the xml document into a variable and then write it into the file需要将xml文件写入变量,然后写入文件

Which looks like看起来像

CREATE tABLE myfile(ID int, FirstName varchar(20),LastName varchar(20))
 EXEC SQL select xmlelement (name "root", xmlelement (name "EmployeeList", XMLAGG ( XMLELEMENT (NAME "Employee", XMLFOREST ( trim(id) as "ID", trim(firstname) as "FirstName", trim(lastname) as "LastName" ) ) ) ) ) INTO:xml_doc FROM myfile; CALL QSYS2.IFS_WRITE( PATH_NAME =>'/myFileInXML.xml', OVERWRITE => 'REPLACE', FILE_CCSID => 1208, LINE => (:xmo__doc ) );

db<>fiddle here db<> 在这里摆弄

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

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