• 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
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    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

  • What You Will Find In IBM i 7.6 TR1 and IBM i 7.5 TR7
  • Three Things For IBM i Shops To Consider About DevSecOps
  • Big Blue Converges IBM i RPG And System Z COBOL Code Assistants Into “Project Bob”
  • As I See It: Retirement Challenges
  • IBM i PTF Guide, Volume 27, Number 41
  • Stacking Up Power11 Entry Server Performance To Older Iron
  • Big Blue Boosts IBM i Support In Instana, Adds Tracing
  • It Is Time To Tell Us What You Are Thinking And Doing
  • IBM i PTF Guide, Volume 27, Number 40
  • The GenAI Boom Is Only Slightly Louder Than The Dot Com Boom

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