• 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
    Manta Technologies

    The Leader in IBM i Education!
    Need training on anything i?
    Manta is all you need.

    130 courses and competency exams on:
    · IBM i operations
    · System Management and Security
    · IBM i Programming Tools
    · Programming in RPG, COBOL, CL, Java
    · Web Development

    SQL, DB2, QueryProduct features:
    · Runs in every popular browser
    · Available 24/7/365
    · Free Student Reference Guides
    · Free Student Administration
    · Concurrent User License
    · Built-In IBM i Simulator

    You can download our 200-page catalog and take sample sessions at MantaTech.com

    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

  • Tool Aims To Streamline Git Integration For Old School IBM i Devs
  • IBM To Add Full System Replication And FlashCopy To PowerHA
  • Guru: Decoding Base64 ASCII
  • The Price Tweaking Continues For Power Systems
  • IBM i PTF Guide, Volume 27, Numbers 31 And 32
  • You Can Now Get IBM Tech Support For VS Code For i
  • Price Cut On Power S1012 Mini Since Power S1112 Ain’t Coming Until 2026
  • IBM i: Pro and Con
  • As I See It: Disruption
  • IBM i PTF Guide, Volume 27, Number 30

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