fhg
Volume 10, Number 12 -- March 31, 2010

Global Temporary Tables and Host Variables

Published: March 31, 2010

by Ted Holt

Global temporary tables are a marvelous, powerful feature of SQL, allowing SQL programmers to create temporary tables as needed in order to build complex queries. One problem you will encounter occurs when you want to use a host variable within a program to limit the amount of data loaded into the table. You may not use a host variable in a Declare Global Temporary Table command. Fortunately, this is an easy limitation to circumvent.

First, let's look at an example of the problem. In the following code fragment, I attempt to create a global temporary table using data for one customer only.

D MyPgm
D  inCompany                     3p 0 const
D  inCustomer                    5p 0 const

 /free
     exec sql
        drop table session/Temp1;
     exec sql
        declare global temporary table Temp1 as
            (select *
               from sales
              where companyno  = :inCompany
                and customerno = :inCustomer)
          with data;

This does not compile. In fact, even SEU won't accept it.

The solution is to create the file and load it in two steps. Here's one possibility.

exec sql
    drop table session/Temp1;
exec sql
    declare global temporary table Temp1 as
       (select * from sales)
     definition only;
exec sql
    insert into session/Temp1
       (select *
          from sales
         where companyno  = :inCompany
           and customerno = :inCustomer);

The phrase "definition only" in the declaration tells the system to create the table based on the fields in the select clause, but not to put any data into the table. Once the table exists, I load it with an insert, which does accept host variables.

I rarely use this technique. In this example, I intentionally kept the select simple, pulling data from one table only, but it would be more realistic to pull data from more than one table, like this:

select h.companyno, h.customerno, h.saledate, h.shipdate,
       h.termcode, t.termdesc,
       co.custname, co.class,
       co.address1, co.address2, co.city,
       co.state, co.postalcode, d.lineno, d.itemno,
       d.quantity, d.price,
       d.quantity * d.price as extended,
       i.itemdesc, i.itemtype, i.itemclass
  from saleshdr as h
  join customer as cu
    on cu.companyno  = h.CompanyNo
   and cu.customerno = h.CustomerNo
  join salesdtl as d
    on h.orderno = d.orderno
  join items as i
    on d.itemno = i.itemno
  join company as co
    on cu.companyno = co.companyno
  join term as t
    on h.termcode = t.termcode
 where companyno  = :inCompany
   and customerno = :inCustomer);

Imagine replacing both of the select statements in the first example with that or worse. The first select would not have to have the where clause, but having to keep the rest of the two instances of the select in sync when changing the query seems less than ideal to me. The first technique is OK for very simple queries, but most queries are not simple.

Here's a second technique that uses similar principles and doesn't require duplication of the select statement.

exec sql
    drop table session/Parms;
exec sql
    declare global temporary table Parms
       (CompanyNo  dec(3,0), CustomerNo dec(5,0));
exec sql
    insert into session/Parms
       values(:inCompany, :inCustomer);
exec sql
    drop table session/Temp1;
exec sql
    declare global temporary table Temp1 as
       (select h.*
          from session/Parms as p
          join sales as h
            on p.companyno  = h.CompanyNo
           and p.customerno = h.CustomerNo)
     with data;

In this example, I create two temporary files--Temp1 to hold the sales data and another one called Parms. The Parms table has only one row (record), into which I load the values of the host variables. From there, I use joins (often an inner join, as in this example, but also often a cross join) to access the host variable values in queries.

I find this technique more useful, especially when the values of the host variables are called for in the building of many temporary tables within a program.

The ability to create global temporary tables is a great boon for SQL programming, and fortunately, the lack for support for host variables in global temporary table definitions is not a show-stopper.




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


Sponsored By
PRODATA COMPUTER SERVICES

Push-Pull-Synchronize Data TODAY!

Finally, a product that provides easy and full SQL
access to remote databases from all System i
high-level languages. Remote Database Connect
gives you easy access to remote databases
from your System i programs.

Share real time data across platforms NOW!

Use RDB Connect today.....
download a free trial NOW.

Order today and SAVE $$$!
800.228.6318

sales@prodatacomputer.com
www.prodatacomputer.com


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Erwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

Help/Systems:  Robot/CONSOLE monitors System i resources automatically
Northeast User Groups Conference:  20th Annual Conference, April 12 - 14, Framingham, MA
COMMON:  Join us at the annual 2010 conference, May 3 - 6, in Orlando, Florida


 

IT Jungle Store Top Book Picks

Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
The iSeries Express Web Implementer's Guide: List Price, $49.95
The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
Can the AS/400 Survive IBM?: List Price, $49.00
Chip Wars: List Price, $29.95


 
The Four Hundred
Power7: Upgrade or Sidestep, Start Planning Now

IBM Flexes Java Muscles on Power7 Iron

IBM Looks Back on 2000s, Sets Sites on Next Decade

As I See It: A Different Currency

Madoff's RPG Coders Indicted in Ponzi Scam

Four Hundred Stuff
CoralTree Toolkit Streamlines CGIDEV2 Web App Development

Info Builders Updates Dashboarding Solution

Unitrends Adds Cloud-Based Storage to Backup Offering

HiT Bolsters Data Synchronization Tool

LTO Tape Drives: More than 3 Million Served

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

System i PTF Guide
March 27, 2010: Volume 12, Number 13

March 20, 2010: Volume 12, Number 12

March 13, 2010: Volume 12, Number 11

March 6, 2010: Volume 12, Number 10

February 27, 2010: Volume 12, Number 09

February 20, 2010: Volume 12, Number 08

TPM at The Register
Ex-contender for top IBM job pleads guilty on securities charges

AMD claims no premium for four-way chips

AMD draws x64 battle lines with 'Magny-Cours'

Intel Xeon 5600 invade big name servers

IBM: Mainframe emulator part of a conspiracy

Sun grows Oracle but stunts profits

HPC propped up server sales in 2009

Reseller exposes Opteron 6100 speeds and prices

QLogic spans Dell's InfiniBand

Red Hat all black in Q4

HP juices gamestations with Intel 'Westmeres'

Dell mainstreams cloud servers

THIS ISSUE SPONSORED BY:

WorksRight Software
ProData Computer Services
Northeast User Groups Conference


Printer Friendly Version


TABLE OF CONTENTS
Variable Procedure Calls in Free-Format RPG

Global Temporary Tables and Host Variables

Implementing 128-Character Passphrases in i/OS

Four Hundred Guru

BACK ISSUES




 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2010 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement