• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: The Three Ways to Insert

    May 8, 2017 Ted Holt

    The INSERT statement is THE (as in the only) SQL way to add new data to a relational database table. At the risk of sounding like a GEICO commercial, “Everybody knows that.” Well, did you know that the INSERT statement supports three distinct ways to add new rows to a table?

    To illustrate the three forms of INSERT, imagine that you and I work for a small company that stores goods in, and ships goods from, a warehouse. Since the company has only one warehouse, there has never been a need for a warehouse ID column in any of the tables or for a warehouse master table. However, that is about to change.

    Business has been good, and the owners have decided to open another warehouse. You and I have to modify the database. We need a table in which to store warehouse data.

    create table warehouses
      ( ID dec(3), Location varchar(16), Code char(3),
        primary key (ID));

    We also add a warehouse ID column to other tables as necessary.

    Method 1: VALUES and Row-value Expressions

    There is no file maintenance program for the new warehouse master table. We’re busy with many other more-important tasks and we don’t have time to write one. For now, we’ll load the first two warehouses by hand. Here’s the command:

    insert into warehouses 
       values (1, 'New Yolk', 'NY'), (2, 'Lost Angeles', 'LA');

    We used the keyword VALUES followed by two row-value expressions. The table looks like this:

    ID Location Code
    1 New Yolk NY
    2 Lost Angeles LA

    Method 2: SELECT

    Time passes and business gets better and better. The owners buy three warehouses from another company. The sellers give us all applicable data, including data for the new (to us) warehouses, in text files. We have loaded those text files into temporary database tables. For historical purposes, management has decided to use the seller’s warehouse ID’s.

    It’s time to load the warehouse data. Here’s the command:

    insert into warehouses (ID, Location, Code)
       select w_id, w_Loc, w_Code 
         from WhsLoad;

    INSERT uses the SELECT command to read data from the load table and add it to the master table. With the addition of the new data, the warehouse master looks like this:

    ID Location Code
      1 New Yolk NY
      2 Lost Angeles LA
    21 Last Vegas LV
    35 Waist Virginia WV
    38 New Hamster NH

    Method 3: VALUES with a Host Structure

    Business continues to improve and we’re opening three more warehouses. Somewhere in the time that has elapsed since the company bought those warehouses, we found a few minutes to write a file maintenance program. Now the appropriate user can add the new warehouses to the database!

    Because we are smart programmers, we did not start from scratch, as if we had never written a file maintenance program before. We cloned an existing RPG program.

    We could have made the file maintenance program insert one row at a time using the first method, but the program we cloned does not work that way. Instead, it can insert one or more rows at a time, taking the data from a data structure.

    D Warehouse       ds                  qualified dim(12)
    D   ID                           3p 0                  
    D   Location                    16a   varying          
    D   Code                         3a                    
                                                           
    D WCount          s              3p 0 inz(3)           
    
    exec sql                                                   
       insert into Warehouses :WCount rows values (:Warehouse);

    This form of INSERT also uses the word VALUES, but VALUES is followed by an array data structure, not one or more row value expressions. The WCOUNT variable tells how many array elements are loaded with data. If the user loads the first three elements, and our program sets WCOUNT to 3 and execute the INSERT command, the table looks like this:

    ID Location Code
      1 New Yolk NY
      2 Lost Angeles LA
      3 South Oklahoma SOK
      4 South Texas STX
      5 St. Lewis SL
    21 Last Vegas LV
    35 Waist Virginia WV
    38 New Hamster NH

    There you have it: one SQL statement with three distinct forms. The more forms, the more power! But everybody knows that.

    RELATED STORIES

    IBM Knowledge Center: INSERT

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: Four Hundred Guru, Guru, IBM i, SQL

    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

    COMMON Opening Session Highlights Excellence IBM Brings Cloud Management In-House with New Console

    3 thoughts on “Guru: The Three Ways to Insert”

    • Ken says:
      May 8, 2017 at 8:38 am

      Ted,

      Thanks for writing this article. I had never seen the use of a data structure in an update statement. I will add it to my tool box and make sure to use it.

      Ken

      Reply
    • Andy Carter says:
      May 9, 2017 at 12:05 am

      Just interested to know….. why the seemingly random assignment of the ID column for the last 3 rows of your sample table? Wouldn’t this typically be auto-generated?

      Reply
    • Ted Holt says:
      November 13, 2017 at 3:46 pm

      It depends on requirements, Andy. If the ID is a meaningless value, such as a surrogate key, then you could have the computer generate it. But if it is an identifier that the corporate office assigns, then you must specify the values. In this imaginary scenario, we can pretend that corporate has selected those warehouse ID’s for historical reasons. That is, that those were the ID numbers that everyone who works in those warehouses is used to, and the ID’s used as foreign keys in the data that comes from those systems. I thought this example was less complicated with assigned ID’s than with generated ones.

      Reply

    Leave a Reply Cancel reply

TFH Volume: 27 Issue: 31

This Issue Sponsored By

  • New Generation Software
  • ProData Computer Services
  • WorksRight Software
  • Computer Keyes
  • Manta Technologies

Table of Contents

  • Watson Puts On A Show At COMMON
  • IBM Brings Cloud Management In-House with New Console
  • Guru: The Three Ways to Insert
  • COMMON Opening Session Highlights Excellence
  • Clearing The Decks Ahead Of The Power9 Launch

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