• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • TR8 DB2 For i Enhancements, Part 1

    November 12, 2014 Michael Sansoterra

    IBM i 7.1 Technology refresh 8 was released on June 6, 2014, and it has another group of small database enhancements worth reviewing. IBM i 7.2 users will find that they already have these features. This tip assumes that you have IBM i 7.2 or IBM i 7.1 with Database Group PTF Level 29 or higher installed.

    QSYS2.GENERATE_SQL Stored Procedure

    This is one sweet new stored procedure IBM has given us. Its purpose is to programmatically retrieve SQL source code into a source member.

    This procedure can accept a boatload of parameters as shown below. Many of the parameters control an aspect of how the code should be retrieved, such as whether or not the OR REPLACE option should be included with a CREATE statement, what DATE_FORMAT should be used, etc.

    Parameter
    Name

    Data
    Type

    Length

    Default

    DATABASE_OBJECT_NAME

    VARCHAR

    258

    No default

    DATABASE_OBJECT_LIBRARY_

    NAME

    VARCHAR

    258

    No default

    DATABASE_OBJECT_TYPE

    VARCHAR

    10

    No default

    DATABASE_SOURCE_FILE_NAME

    VARCHAR

    10

    ‘Q_GENSQL’

    DATABASE_SOURCE_FILE_

    LIBRARY_NAME

    VARCHAR

    10

    ‘QTEMP’

    DATABASE_SOURCE_FILE_

    MEMBER

    VARCHAR

    10

    ‘Q_GENSQL’

    SEVERITY_LEVEL

    INTEGER

     

    39

    REPLACE_OPTION

    CHARACTER

    1

    ‘1’

    STATEMENT_FORMATTING_

    OPTION

    CHARACTER

    1

    ‘1’

    DATE_FORMAT

    CHARACTER

    3

    ‘ISO’

    DATE_SEPARATOR

    CHARACTER

    1

    ‘-‘

    TIME_FORMAT

    CHARACTER

    3

    ‘ISO’

    TIME_SEPARATOR

    CHARACTER

    1

    ‘.’

    NAMING_OPTION

    CHARACTER

    3

    ‘SQL’

    DECIMAL_POINT

    CHARACTER

    1

    ‘.’

    STANDARDS_OPTION

    CHARACTER

    1

    ‘0’

    DROP_OPTION

    CHARACTER

    1

    ‘0’

    MESSAGE_LEVEL

    INTEGER

     

    0

    COMMENT_OPTION

    CHARACTER

    1

    ‘1’

    LABEL_OPTION

    CHARACTER

    1

    ‘1’

    HEADER_OPTION

    CHARACTER

    1

    ‘1’

    TRIGGER_OPTION

    CHARACTER

    1

    ‘1’

    CONSTRAINT_OPTION

    CHARACTER

    1

    ‘1’

    SYSTEM_NAME_OPTION

    CHARACTER

    1

    ‘1’

    PRIVILEGES_OPTION

    CHARACTER

    1

    ‘1’

    CCSID_OPTION

    CHARACTER

    1

    ‘1’

    CREATE_OR_REPLACE_OPTION

    CHARACTER

    1

    ‘0’

    OBFUSCATE_OPTION

    CHARACTER

    1

    ‘0’

    ACTIVATE_ROW_AND_COLUMN

    _ACCESS_CONTROL_OPTION

    CHARACTER

    1

    ‘1’

    MASK_AND_PERMISSION_

    OPTION

    CHARACTER

    1

    ‘1’

    QUALIFIED_NAME_OPTION

    CHARACTER

    1

    ‘0’

    ADDITIONAL_INDEX_OPTION

    CHARACTER

    1

    ‘0’

    INDEX_INSTEAD_OF_VIEW_

    OPTION

    CHARACTER

    1

    ‘0’

    All of the parameters are input only, and remember, all parameters with defaults may be omitted. If you need help understanding some of the parameters, just see the IBM developerWorks page that describes this procedure.

    Here are a few examples of how to use it. Note that with so many parameters, it’s advantageous to call the procedure using named arguments:

    -- 
    -- Available in DB2 for i 7.1 Group PTF Level 29 or IBM i 7.2
    CALL QSYS2.GENERATE_SQL('%', 'ADVWORKS', 'TABLE', 
    REPLACE_OPTION => '0');
    CALL QSYS2.GENERATE_SQL('%', 'ADVWORKS', 'PROCEDURE', 
    REPLACE_OPTION => '1');
    -- The file and member must already exist 
    CALL QSYS2.GENERATE_SQL('%', 'ADVWORKS', 'PROCEDURE', 
    REPLACE_OPTION => '1', 
    DATABASE_SOURCE_FILE_NAME=>'QSQLSRC',
    DATABASE_SOURCE_FILE_LIBRARY_NAME=>'DEV',
    DATABASE_SOURCE_FILE_MEMBER => 'PROCEDURES');
    

    The specified source file name and source member must already exist. The % wildcard character can be used in the schema name and the object name.

    In this example, all function definitions are retrieved from any library that begins with SYS, such as SYSIBM and SYSIBMADM:

    -- Get all functions in libraries that begin with SYS
    CALL QSYS2.GENERATE_SQL('%', 'SYS%', 'FUNCTION', 
    REPLACE_OPTION => '1', 
    DATABASE_SOURCE_FILE_NAME=>'QSQLSRC',
    DATABASE_SOURCE_FILE_LIBRARY_NAME=>'DEV',
    DATABASE_SOURCE_FILE_MEMBER => 'SYSFUNCS');
    

    While the QSQGNDDL API can also retrieve SQL source code, this procedure is much simpler to use. IBM has done a great job of giving DB2 coders features formerly only available using IBM i OS or a high level language programming language. It’s a great thing when a DB2 database developer doesn’t have to delve into platform specific items to accomplish a task.

    This feature can really help administrators and developers by allowing them to retrieve source code programmatically. Of course, if the procedure or function source has been obfuscated, this procedure will not retrieve the source code.

    If I could make a wish for an enhancement, it would be that a procedure like this would allow the user to optionally specify the creation of an INSERT or UPDATE template statement based on a given table or view name. When I develop in SQL Server Management Studio, I use a similar feature and it saves quite a bit of time. If you’ve ever had to manually code an INSERT statement against a table with a truckload of columns, you can imagine what a time saver it would be to have the column names mapped out already.

    QSYS2.DELIMIT_NAME Scalar Function

    The DELIMIT_NAME function accepts a string expression and will return delimiters (as necessary) to make the expression a valid DB2 column name. For example, normally you can’t name or alias a column expression with a name like % Total. However, with delimiters (DB2 uses the double quote character), DB2 will accept the delimited column name “% Total” as valid. That’s cool that you can have crazy names in DB2 but it can be a pain in the neck because to reference them in a query you have to remember they’re case sensitive, the delimiters are required, etc.

    DELIMIT_NAME will accept a string expression representing a column name and see if it conforms to the DB2 naming rules (valid characters, no embedded spaces, etc.). If it conforms, the function will simply return the name passed to it. If it doesn’t conform, then the function will add the DB2 delimiter (double quote) around the name.

    This function is useful when constructing dynamic SQL. For example, if you have a report writing application that allows the user to create queries with specific names for the columns, this function would allow the user to define friendly column names (for something like Excel headings) without making SQL choke! This function can also be used to validate whether or not a name is a valid for DB2.

    This SQL statement:

    VALUES (DELIMIT_NAME('Sales Order')),
           (DELIMIT_NAME('TOTAL$')),
           (DELIMIT_NAME('PRODUCT LENGTH (")')),
           (DELIMIT_NAME('%Rank***'));
    

    Returns the following results:

    "Sales Order"	
    TOTAL$	
    "PRODUCT LENGTH ("")"	
    "%Rank***"	
    

    Reference: QSYS2.DELIMT_NAME UDF

    QSYS2.RESTART_IDENTITY() Procedure

    This procedure gives developers and administrators the ability to reset a DB2 identity column’s next value. For example, if you use the CPYF command to copy a table with an identity column to a new table, you’ll unpleasantly find that the destination table’s identity column starts with the original initial value. That’s a recipe for a duplicate key value error.

    Say you copy file DATALIB/ITEMMASTER (with an INTEGER identity column) to DEV/ITEMMASTER2. The following procedure call will resync the next identity value from the old table to the new table:

    CALL QSYS2.RESTART_IDENTITY(
    'DATALIB', 'ITEMMASTER', 'DEV', 'ITEMMASTER2') 
    

    As shown by the parameters, the source table’s identity column attributes are placed on the destination table’s identity column.

    I’m not sure why IBM did it this way instead of just letting you reset the identity column’s next value to whatever you’d like. I’d like to see a procedure that simply lets you set the next value for a table. However, with a little finagling, you can almost do that. Let’s say DEV/ITEMMASTER2 has an identity column called ITEM_ID and that you want the next ID value to be 100,000. You can create a dummy table containing an identity column of the same name of the table you want to change, and then call RESTART_IDENTITY:

    CREATE TABLE DEV.ID_DUMMY 
    (ITEM_ID INT AS IDENTITY 
    (START WITH 100000 INCREMENT BY 1) 
    NOT NULL PRIMARY KEY);
    
    -- Reset DEV/ITEMMASTER2 next id (using system name ITEMM00001)
    CALL QSYS2.RESTART_IDENTITY(
    SOURCE_SYSTEM_TABLE_SCHEMA=>'DEV', 
    SOURCE_SYSTEM_TABLE_NAME=>'ID_DUMMY',
    TARGET_SYSTEM_TABLE_SCHEMA=>'DEV',
    TARGET_SYSTEM_TABLE_NAME=>'ITEMM00001'); 
    

    Now, DEV/ITEMMASTER2’s next identity value will be 100,000.

    Reference: QSYS2.RESTART_IDENTITY

    Run SQL Scripts

    With the June 2014 release of System i Access (SI53584), the Run SQL Scripts utility will allow you to save all of the result sets when running a stored procedure (or multiple SELECT queries run in a script) that return multiple result sets. In prior releases, the “Save Results” option was not available when multiple result sets were returned. Although not officially part of TR8, the timing of this service pack was close to TR8 so I thought I’d mention it as I find myself working with multiple result set procedures quite frequently.

    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 STORY

    Protect Your Intellectual Property: Obfuscate DB2 For i Source Code



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Computer Keyes

    Fax Directly from your IBM i

    KeyesFax is a full function automated IBM i fax system. Spooled files are burst by fax number and auto transmitted with overlays.  It combines both a send and receive facsimile processing system with a complete image package.

    The fax software will edit, send, receive, display, print, and track fax documents or images using any standard IBM i without additional expensive hardware, software or subscriptions.

    Computer Keyes has been developing Software Solutions since 1978!

    www.computerkeyes.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    NGS:  FREE, Live Webinar: Reporting & Analytics on IBM i for Manufacturers & Distributors. Nov 18
    MiNET:  Automate PDF, email, Fax, Archiving & more with ArtForm400. Try us for a FREE Redbox code!
    ASNA:  Powerful IBM i apps shouldn't require a painful learning curve. Get the ASNA Wings white paper.

    More IT Jungle Resources:

    System i PTF Guide: Weekly PTF Updates
    IBM i Events Calendar: National Conferences, Local Events, and Webinars
    Breaking News: News Hot Off The Press
    TPM @ EnterpriseTech: High Performance Computing Industry News From ITJ EIC Timothy Prickett Morgan

    IBM Builds Infrastructure Resource Site The Windows Of Opportunity

    Leave a Reply Cancel reply

Volume 14, Number 25 -- November 12, 2014
THIS ISSUE SPONSORED BY:

SEQUEL Software
ProData Computer Services
WorksRight Software

Table of Contents

  • TR8 DB2 For i Enhancements, Part 1
  • SQL Functions You Didn’t Know You Had, Part 2
  • Flip This Job Number: Adjusting The Job Queue Control Utility For Job Number Resets

Content archive

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

Recent Posts

  • IBM Unveils Manzan, A New Open Source Event Monitor For IBM i
  • Say Goodbye To Downtime: Update Your Database Without Taking Your Business Offline
  • i-Rays Brings Observability To IBM i Performance Problems
  • Another Non-TR “Technology Refresh” Happens With IBM i TR6
  • IBM i PTF Guide, Volume 27, Number 18
  • Will The Turbulent Economy Downdraft IBM Systems Or Lift It?
  • How IBM Improved The Database With IBM i 7.6
  • Rocket Celebrates 35th Anniversary As Private Equity Owner Ponders Sale
  • 50 Acres And A Humanoid Robot With An AI Avatar
  • IBM i PTF Guide, Volume 27, Number 17

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