• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Copy Data From A Remote DB2 Database Using DB2 For i 7.1

    July 25, 2012 Michael Sansoterra

    IBM DB2 for i 7.1 introduced the ability to query a remote DB2 for i database directly without having to invoke an explicit CONNECT statement. Querying a remote table with one SQL statement is great, but lacks one important feature: requests for remote data are usually combined with requests for local data.

    However, DB2 for i does not allow references to tables from multiple relational databases in a single statement, until now that is. The i7.1 technology refresh 4 update allows an INSERT/SELECT statement to reference tables from different DB2 relational databases. (Even if you’re not at i7.1 you can still perform this task.)

    Say, for example, your company has two IBM i machines (a.k.a., iSeries, System i, AS/400) with relational databases named QERP (ERP) and QINV (Inventory), and that these machines need to share item master data. In the past, you could accomplish data sharing using FTP, DDM, or embedded SQL using DRDA (to name a few techniques).

    This new feature allows a developer to write a single INSERT/SELECT statement (executing on QERP) that can query remote relational database QINV and place the results in a local table on QERP. The bonus is a CONNECT statement isn’t required either.

    Assuming a developer is coding a data retrieval routine on system QERP, a statement to copy QINV’s item master data to a local QTEMP table is as simple as this:

    INSERT INTO QTEMP/ITEM_MASTER 
    SELECT * 
      FROM QINV/DEV/ITEM_MASTER IM
    

    This statement uses the three-part naming convention that consists of the relational database name/schema (library) name/table-or-view name. When using the SQL naming convention, use a dot separator instead of the slash.

    There are a few things to keep in mind when the SQL naming convention:

    • This technique uses DRDA to communicate with the remote relational database and does an implicit CONNECT behind the scenes. Therefore, DRDA should be configured appropriately. If you’re not sure how to configure your local and remote machines to communicate using DRDA, review the references section below.
    • The local IBM i must have a registered relational database entry for the remote machine. In this case, machine QIBM must have a relational database directory entry for QINV. Use the Add Relational Database Directory Entry (ADDRDBDIRE) command to add a relational database reference to the remote machine or partition.
    • Currently, this technique can only be used with an INSERT/SELECT statement where the target of the INSERT is on the local machine and all tables in the SELECT refer to the same remote relational database. Other statements that mix references to multiple relational databases will return the error: SQL0512 – Statement references objects in multiple databases. Hopefully one day soon, developers will be able to reference multiple relational databases in other SQL DML statements.
    • If you don’t mind writing embedded SQL or a SQL stored procedure, other SQL statements such as UPDATE, SELECT, CREATE TABLE AS, etc., can benefit from this feature by simply coding an INSERT/SELECT from the remote database into a temporary table. Thereafter, once the data is in a QTEMP table, a secondary SQL statement such as an UPDATE can make use of the temporary data copied from the remote system.
    • Only the “client” (i.e., local) IBM i machine needs to be at i7.1 with Technology Refresh 4 for this feature to work. The “server” (i.e., remote) IBM i can be on an earlier version of IBM i/OS. (I verified it works against i6.1.)
    • When connecting to other systems, remember the SELECT statement will run on the remote box. Therefore you can only reference SQL features available on the remote box’s i/OS release. For example, when connecting to an i6.1 machine, you cannot use the new i7.1 built-in functions.
    • When referencing remote routines like user-defined functions in the SELECT, include the schema name when using the SQL naming convention, for example: INSERT INTO LOCAL_TABLE SELECT MYLIB.MYFUNCTION() ….
    • The SYSTEM_USER and CURRENT_SERVER special registers can be used to query the remote user and remote server names.

    One more thing, if you’re not on i7.1, there has always been a relatively easy way to do this type of INSERT local/SELECT remote on older systems. To do so, follow these steps:

    1. Make sure there is a remote database entry configured on the local box (see references section below).

    2. Enter the SQL SELECT statement for a remote database query in a source member. Do not use the three part naming syntax.

    3. Create a query management query (QMQRY) from the source member using the CRTQMQRY command:

    CRTQMQRY QMQRY(REMOTE_QRY)  SRCFILE(SRCLIB/QQMQRYSRC)
    

    This command will create a query management query object containing the SQL definition from the source member.

    4. Execute the QMQRY against the remote server and dump the results locally to an outfile:

    STRQMQRY QMQRY(REMOTE_QRY) OUTPUT(*OUTFILE) 
    OUTFILE(QTEMP/REMOTE_DTA) RDB(MYREMOTE) USER(MIKE)   PASSWORD(T0@ST)
    

    Overall, IBM just brought developers one step closer to the idealized dream of being able to access and manipulate data from heterogeneous systems with a single SQL statement. In a future tip, I’ll explore a mechanism that allows local and multiple remote relational database tables (DB2 only) to be referenced in a single SQL SELECT statement.

    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 AND REFERENCES

    Using DRDA to Execute SQL Statements on Another iSeries

    Inserting Data From A Remote Database, IBM i SQL programming manual

    Distributed Database Programming, IBM i manual



                         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
    Maxava

    Migrate IBM i with Confidence

    Tired of costly and risky migrations? Maxava Migrate Live minimizes disruption with seamless transitions. Upgrading to Power10 or cloud hosted system, Maxava has you covered!

    Learn More

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Townsend Security:  View the recorded Webcast: Secure Managed File Transfers for the IBM i
    Help/Systems:  FREE: Download the IBM i Scheduling Survival Guide
    Abacus Solutions:  More affordable and flexible alternatives to deliver secondary workloads

    IT Jungle Store Top Book Picks

    BACK IN STOCK: Easy Steps to Internet Programming for System i: List Price, $49.95

    The iSeries Express Web Implementer's Guide: List Price, $49.95
    The iSeries Pocket Database Guide: List Price, $59
    The iSeries Pocket SQL Guide: List Price, $59
    The iSeries Pocket WebFacing Primer: List Price, $39
    Migrating to WebSphere Express for iSeries: List Price, $49
    Getting Started with WebSphere Express for iSeries: List Price, $49
    The All-Everything Operating System: List Price, $35
    The Best Joomla! Tutorial Ever!: List Price, $19.95

    Introducing the IBM Intranet Experience IBM i Wins Software Pricing Throwdown Versus AIX-DB2 Combo

    Leave a Reply Cancel reply

Volume 12, Number 19 -- July 25, 2012
THIS ISSUE SPONSORED BY:

WorksRight Software
T.L. Ashford
System i Developer

Table of Contents

  • Is An RPGOA-like Standard For HTML5 On The Horizon?
  • Copy Data From A Remote DB2 Database Using DB2 For i 7.1
  • Admin Alert: The Right Way To Delete User Profiles, Part 1

Content archive

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

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

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