• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Table Value Constructors Build Tables On The Fly

    June 25, 2018 Ted Holt

    Because I wish to be as valuable and productive as possible to the people who pay me to program their computers, I continually search the Web for new ideas and techniques. Doing so often leads me to sites that cater to other computing platforms. Today I want to share with you some SQL techniques that I learned from Microsoft SQL Server professionals.

    These techniques are based on the Table Value Constructor (TVC), which is a group of data values, usually literals, organized into rows and columns. In its simplest form, a table value constructor is literal tabular data that is not stored in a database table.

    A table value constructor begins with the word VALUES, which is followed by one or more row value expressions.

    Assume REPS, a table of sales representatives, which contains decimal columns REPID and REGION. We can use a table value constructor to add a group of sales reps.

    insert into reps values
    (11, 1), (12, 3), (17, 4),
    (25, 2), (27, 2), (28, 4)
    

    Six sales reps have been assigned to regions 1 through 4.

    REPID REGION
    11 1
    12 3
    17 4
    25 2
    27 2
    28 4

    After a period of booming business and expansion into new parts of the globe, management revises the list of regions. This means that more region numbers are defined and some existing regions are assigned different region numbers. How would we go about reassigning the reps to the new region numbers?

    We could build a table of old and new region numbers and use that table as the source in a MERGE statement, as I wrote about in Updating Through A Join, Take Three. If I don’t need such a table for other purposes, I can use a table value constructor instead, like this:

    merge into reps as r
    using (values (1, 1), (2, 6), (3,4), (4,2))
      as changes (OldRegion, NewRegion)
      on r.region = changes.OldRegion
    when matched then
       update set r.Region = changes.NewRegion
    

    The system reassigned the reps using the literal values in the TVC.

    REPID REGION
    11 1
    12 4
    17 2
    25 6
    27 6
    28 2

    I learned something else that was interesting. I found out that I can use subselects instead of literals in table value constructors. Here’s a contrived example:

    insert into reps values (
    (select repid from shipments where shipmentid = 202), 4);
    

    The REPID value is retrieved from shipment 202, and whatever rep that is gets assigned to region 4.

    I haven’t use this technique in production yet, so if you do, please share your code with me. Also, if you have a TVC technique that I didn’t mention, please email me so I can share it with other readers.

    I’ve yet to see the terms table value constructor and TVC used in IBM literature, but I expect IBM to start using it one of these days. After all, how else will they let SQL Server users know that DB2 also has this feature?

    RELATED STORIES

    Updating Through A Join, Take Three

    Table Value Constructor (Transact-SQL)

    Table Value Constructors in SQL Server 2008

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: DB2, IBM i, SQL, table value constructor, TVC

    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

    A Better Way To Skin The IBM i Cloud Cat Trinity Guard Gives Audit Tool A Friendly GUI

    2 thoughts on “Guru: Table Value Constructors Build Tables On The Fly”

    • Dietmar Bürkle says:
      July 4, 2018 at 12:42 pm

      Hi Ted,

      maybe you missed this article:
      Treasury Of New DB2 6.1 Features, Part 1: Query Enhancements
      https://www.itjungle.com/2009/02/04/fhg020409-story02/

      Dietmar

      Reply
    • Ted Holt says:
      July 30, 2018 at 8:14 am

      We revisit topics when we believe we can add to what we’ve already published.

      Reply

    Leave a Reply Cancel reply

TFH Volume: 28 Issue: 44

This Issue Sponsored By

  • Profound Logic Software
  • ARCAD Software
  • RPG & DB2 Summit
  • OCEAN TechCon18
  • WorksRight Software

Table of Contents

  • Lamps Plus Sheds Light On Modernization Integration
  • Trinity Guard Gives Audit Tool A Friendly GUI
  • Guru: Table Value Constructors Build Tables On The Fly
  • A Better Way To Skin The IBM i Cloud Cat
  • IT Spending To Boom In 2018, Tails Off In 2019

Content archive

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

Recent Posts

  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19
  • 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

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