• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • CASE Simplifies SQL Update

    September 29, 2010 Hey, Ted

    I need to run a series of similar updates against 12 groups in a very large physical file (millions of records). Is there a way I can combine the updates into one SQL update command? I’d rather have the system read the file once, not 12 times.

    –Jim

    Here’s an example that illustrates Jim’s update. Set the minimum order quantity to 12 for all items in class A1.

    update items
       set minordqty = 12
     where class = 'A1'
    

    If Jim has 12 class values, he has to run 12 update commands. However, if he were to use a CASE structure, he would only need one update command. To illustrate, here’s the same update with five classes.

    update items
       set minordqty =
              case class
                 when 'A1' then  12
                 when 'B1' then 144
                 when 'B2' then 144
                 when 'D1' then  24
                 when 'D2' then  12
                 else 12
              end
    

    Case opens up some powerful opportunities. Here’s another example.

    Suppose you need two updates to the item master file. You want to update the minimum order quantity using one set of criteria, and update the discount code using an unrelated set of criteria. You could run two SQL update commands, each with its own where clause, but try this instead.

    update items
      set minordqty =
             case class
                when 'A1' then  12
                when 'B1' then 144
                when 'B2' then 144
                when 'D1' then  24
                when 'D2' then  12
                else 12
             end,
          discount =
             case
                when MakeBuy = 'M' then 1
                when VMI = 'Y'     then 3
                else 2
             end
    

    Of course, there’s no reason you can’t use a where clause too, if it suits your purposes. Here’s the previous query, but it only updates records that are marked as active.

    update items
      set minordqty =
             case class
                when 'A1' then  12
                when 'B1' then 144
                when 'B2' then 144
                when 'D1' then  24
                when 'D2' then  12
                else 12
             end,
          discount =
             case
                when MakeBuy = 'M' then 1
                when VMI = 'Y'     then 3
                else 2
             end
     where active = 'Y'
    

    –Ted



                         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
    Midrange Dynamics North America

    With MDRapid, you can drastically reduce application downtime from hours to minutes. Deploying database changes quickly, even for multi-million and multi-billion record files, MDRapid is easy to integrate into day-to-day operations, allowing change and innovation to be continuous while reducing major business risks.

    Learn more.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    PowerTech:  FREE Webinar! Reduce the Cost and Effort of IBM i Auditing. Sept. 29, 10 a.m. CT
    LANSA:  2010 iPulse Survey. Taking the pulse of the IBM i market. Get a chance to win an iPad!
    COMMON:  Join us at the Fall 2010 Conference & Expo, Oct. 4 - 6, in San Antonio, Texas

    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

    Q Software to Widen Market for Security Tools The Little Power7 Engines That Could–And Those That Won’t

    Leave a Reply Cancel reply

Volume 10, Number 29 -- September 29, 2010
THIS ISSUE SPONSORED BY:

WorksRight Software
iSeries DevCon2010
inFORM Decisions

Table of Contents

  • RPG Sorting and Searching: A 7.1 Update
  • CASE Simplifies SQL Update
  • Feeding the Auditor: Taking Care of Problem User Profiles

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