• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Assorted DB2 for i Questions

    August 16, 2016 Michael Sansoterra

    Shown below are assorted questions related to DB2 for i. If you have a question you’d like Four Hundred Guru to try to answer, be sure to visit the IT Jungle Contact page. Note that some of these questions were modified from their original form for clarity and simplicity.


    Adding an attribute to an element based XML

    Q: The FHG tips on composing XML documents from DB2 data (Part 1 and Part 2) do not describe how to add a custom attribute to element based XML. Consider the following query:

    SELECT XMLAGG(
           XMLROW(NAME as "Name",
                  Color as "Color",
                  ProductNumber as "ProductId"
                  OPTION ROW "Product"))
    AS PRODUCT_XML
    FROM (
    SELECT * FROM ADVWORKS12.PRODUCT
    ) p
    

    to generate this element based XML:

    <Product>
      <Name>LLCrankarm</Name>
      <Color>Black</Color>
      <ProductId>CA-5965</ProductId>
    </Product>
    

    But, what if I want the product ID to be an attribute of the product element as shown below?

    <Product ID="CA-5965">
      <Name>LL Crankarm</Name>
      <Color>Black</Color>
    </Product>
    

    A. As far as I can tell, there is no way to combine element and attribute based XML using the XMLROW function as demonstrated in the referenced Guru tips. However, by using the XMLELEMENT and XMLATTRIBUTES functions, the desired XML structure can be built as follows:

    SELECT XMLAGG(
           XMLELEMENT(NAME "Product",
             XMLATTRIBUTES(ProductNumber AS ID),
             XMLELEMENT(NAME "Name",Name),
             XMLELEMENT(NAME "Color",Color)
           ))
    FROM (
    SELECT * FROM ADVWORKS12.PRODUCT 
    ) p
    

    The downside of abandoning XMLROW is that the SQL statement can become quite large when using several XMLELEMENT functions for a large number of columns.


    Deadlock handling differences between DB2 for i and DB2 LUW

    Q: Does DB2 for i handle deadlocks in the same way as DB2 LUW?

    A. For background, I present the Wikipedia deadlock definition: “In a transactional database, a deadlock occurs when two processes, each within its own transaction, updates two rows of data (that is, records) but in the opposite order.”

    In DB2 LUW (and many other database engines such as SQL Server) when the database engine detects a deadlock it will kill one of the deadlocked processes and automatically rollback the transaction that has done the least amount of work. In DB2 for LUW, SQL0911 message is issued when a deadlock occurs:

    The current transaction has been rolled back because of a deadlock or timeout
    

    This message ID doesn’t exist in QSQLMSG in DB2 for i 7.2. In fact, I don’t see any references to the term deadlock in the SQL Reference manual. Further, when I create an artificial deadlock scenario between two SQL sessions in DB2 for i, I receive an SQL0913 (row or object in use) error in one of the sessions with no automatic rollback.

    In short, I have never witnessed a deadlock in DB2 for i and it doesn’t look like it has the capacity for deadlock handling. I’m not sure why it doesn’t support deadlock handling similar to other DB2 products, but apparently the best it can do is issue an “object in use” error after the default timeout occurs. By default, this error provides the system operator with the opportunity to cancel or retry the process.

    If you’re programming a cross-platform DB2 application, make sure to put in an error handler for the SQL0913 to support DB2 for i.


    TIMESTAMP function behavioral differences among different SQL Environments

    Q. I’m running IBM i 7.1 TR 11. The following statement fails in STRSQL but succeeds in System i Navigator:

    VALUES TIMESTAMP(CHAR(LAST_DAY(DATE(CURRENT_DATE-1 MONTHS)))||'-21.05.00')
    

    What gives?

    A. The answer to this perplexing conundrum lies in the differences in the default date format (*DATFMT) setting. System i Navigator defaults this value to *ISO (yyyy-mm-dd) whereas STRSQL for a standard US IBM i is *MDY.

    When accepting a character value as a parameter, the TIMESTAMP function is somewhat restrictive in what it accepts as a valid string representation. The date portion of the string should be in *ISO format in order for TIMESTAMP to accept the string representation and convert it to an actual timestamp data type.

    However, the CHAR function will convert the date expression into a *MDY format value that the TIMESTAMP function will reject. One easy way to make sure the behavior is consistent among SQL environments running with different date formats is to force the CHAR function to always format the date in the ISO format by adding the optional ISO date format parameter:

    VALUES TIMESTAMP(CHAR(LAST_DAY(DATE(CURRENT_DATE-1 MONTHS)),ISO)||'-21.05.00')
    

    When the date portion is consistently in ISO format, the TIMESTAMP function will happily accept a string representation without complaining.

    Michael Sansoterra is a DBA for Broadway Systems in Grand Rapids, Michigan. Send your questions or comments for Mike via the IT Jungle Contact page.

    RELATED STORIES

    Inline Table Functions In DB2 For i

    Surge of Services in DB2 for i, Part 2

    Surge Of Services In DB2 For i, Part 1

    Native Regular Expressions In DB2 For i 7.1 And 7.2

    Gimme, Gimme, Gimme! More Services And Global Variables Available For DB2 for i

    DB2 for i 7.2 Functions, Functions, Functions

    Composing An XML Document From Relational Data, Part 1

    Composing An XML Document From Relational Data: Part 2

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    DRV Tech

    Get More Out of Your IBM i

    With soaring costs, operational data is more critical than ever. IBM shops need faster, easier ways to distribute IBM applications-based data to users more efficiently, no matter where they are.

    The Problem:

    For Users, IBM Data Can Be Difficult to Get To

    IBM Applications generate reports as spooled files, originally designed to be printed. Often those reports are packed together with so much data it makes them difficult to read. Add to that hardcopy is a pain to distribute. User-friendly formats like Excel and PDF are better, offering sorting, searching, and easy portability but getting IBM reports into these formats can be tricky without the right tools.

    The Solution:

    IBM i Reports can easily be converted to easy to read and share formats like Excel and PDF and Delivered by Email

    Converting IBM i, iSeries, and AS400 reports into Excel and PDF is now a lot easier with SpoolFlex software by DRV Tech.  If you or your users are still doing this manually, think how much time is wasted dragging and reformatting to make a report readable. How much time would be saved if they were automatically formatted correctly and delivered to one or multiple recipients.

    SpoolFlex converts spooled files to Excel and PDF, automatically emailing them, and saving copies to network shared folders. SpoolFlex converts complex reports to Excel, removing unwanted headers, splitting large reports out for individual recipients, and delivering to users whether they are at the office or working from home.

    Watch our 2-minute video and see DRV’s powerful SpoolFlex software can solve your file conversion challenges.

    Watch Video

    DRV Tech

    www.drvtech.com

    866.378.3366

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    System i Developer:  RPG & DB2 Summit - October 4-6 2016 in Chicago. Register now!
    COMMON:  Open Source and Systems Management at the COMMON Forum. August 24-25 in Chicago.
    Four Hundred Monitor Calendar:  Latest info on national conferences, local events, & Webinars

    New Round Of Enhancements For Access Client Solutions Ublu: A Modern Band-Aid for Legacy i Ills

    Leave a Reply Cancel reply

Volume 16, Number 18 -- August 16, 2016
THIS ISSUE SPONSORED BY:

WorksRight Software
Baseline Data Services
COMMON

Table of Contents

  • Overlaid Packed Data In Data Structures
  • How To Insert With A Common Table Expression
  • Assorted DB2 for i Questions

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • The Power11 Transistor Count Discrepancies Explained – Sort Of
  • Is Your IBM i HA/DR Actually Tested – Or Just Installed?
  • Big Blue Delivers IBM i Customer Requests In ACS Update
  • New DbToo SDK Hooks RPG And Db2 For i To External Services
  • IBM i PTF Guide, Volume 27, Number 33
  • Tool Aims To Streamline Git Integration For Old School IBM i Devs
  • IBM To Add Full System Replication And FlashCopy To PowerHA
  • Guru: Decoding Base64 ASCII
  • The Price Tweaking Continues For Power Systems
  • IBM i PTF Guide, Volume 27, Numbers 31 And 32

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle