• 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
    CloudSAFE

    CloudSAFE – for secure, scalable hosting and managed services for IBM i environments, delivering high availability, 24×7 monitoring, backup, recovery, and expert support to modernize operations, reduce risk, and ensure always-on performance reliably.

    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

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

    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

  • Bob 1.0 Users Bugged By Lack Of One Feature
  • Here Come The AI-Based Code Modernization Offerings
  • Guru: Cohesion First – What A Procedure Should Be Responsible For
  • IBM Offers Trade-Ins On Storage To Grease The Upgrade Skids
  • IBM i PTF Guide, Volume 28, Number 14
  • What IBM i Ideas Are Cooking In IBM’s Ideas Portal?
  • Early Bob Excels In Medhost IBM i Tryout
  • Counting The Cost Of AI Inference – And Projecting It Far Out
  • IBM i PTF Guide, Volume 28, Number 13
  • The Next Generation Of IBM i Talent in GenAI Action

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