fhg
Volume 11, Number 14 -- April 27, 2011

Updating Through a Join with SQL, Take Two

Published: April 27, 2011

Hey, Ted:

I am studying your tip Updating through a Join with SQL, and something escapes me. I do not understand the redundancy in the first code example. Do all updates that involve joins have to have redundant code?

--Ron


I know it's confusing, Ron. I can go into more detail for you.

This is the example to which you refer:

update customer as c
   set region =
      (select newregion
         from fixes
        where oldregion = c.region)
 where exists
      (select *
         from fixes
        where oldregion = c.region)

You're right that there is some redundancy. Both the SET and WHERE clauses of the UPDATE statement refer to the FIXES table.

         from fixes
        where oldregion = c.region)

The direct answer to your question is "no". Updates that involve joins do not necessarily have to have redundant code. It depends on what you wish to accomplish. If you want the UPDATE to change all records (rows) in the database file (table or view), then there is no need for a WHERE clause, because the purpose of the WHERE clause is to specify which rows are to be updated. But if you only want to update some of the rows, then yes, you must include a WHERE clause in the UPDATE statement.

Let me illustrate.

Suppose you have a master file of vendors that includes a vendor-type code. The code has never been loaded, but now you need to update the code based on a file that was created from a user's spreadsheet. The master file is VENDORS. The file of updates is called VENDORUPDT, and it has two fields only: vendor ID number and type code. Let's consider some scenarios.

1. VENDORUPDT has a row for each vendor. All rows in VENDOR will be updated.

In this case, there is no need for a WHERE clause in the UPDATE statement.

update vendors as v1
   set v1.type =
         (select v2.type
            from vendorupdt as v2
           where v2.id = v1.id)

2. VENDORUPDT does not have a row for some vendors. All rows in VENDOR will be updated. Vendors who are not referenced in VENDORUPDT will be given a null type code.

Since all rows are to be updated, there is no WHERE clause in the UPDATE.

update vendors as v1
   set v1.type =
         (select v2.type
            from vendorupdt as v2
           where v2.id = v1.id)

3. VENDORUPDT does not have a row for some vendors. All rows in VENDOR will be updated. Vendors who are not referenced in VENDORUPDT will be given a type value of 2.

Since all rows are to be updated, there is no WHERE clause in the UPDATE.

update vendors as v1
   set v1.type = coalesce(
         (select v2.type
            from vendorupdt as v2
           where v2.id = v1.id), 2)

4. VENDORUPDT does not have a row for some vendors. Rows in VENDOR will be updated only if they are found in VENDORUPDT.

Since some rows will not be updated, the UPDATE needs a WHERE clause.

update vendors as v1
   set v1.type = 
         (select v2.type
            from vendorupdt as v2
           where v2.id = v1.id)
 where v1.id in
       (select v3.id
          from vendorupdt as v3)

5. VENDORUPDT does not have a row for some vendors. Rows in VENDOR will be updated only if they are found in VENDORUPDT and if they are located in the state of Mississippi.

Since some rows will not be updated, the UPDATE needs a WHERE clause.

update vendors as v1
   set v1.type = 
         (select v2.type
            from vendorupdt as v2
           where v2.id = v1.id)
 where v1.state = 'MS' 
   and v1.id in
       (select v3.id
          from vendorupdt as v3)

Notice that the last two examples contain WHERE clauses, but not the redundancy you noticed in the example from the previous tip.

To sum it up, the WHERE clause specifies which rows are to be updated. It may be redundant with the expression in SET, but it doesn't have to be.

--Ted


RELATED STORIES

Updating through a Join with SQL

Update One File Based on Another File



                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
SEQUEL SOFTWARE

Still using a green screen to design SEQUEL views?

Discover how to make data access easier and more efficient.

In less than an hour, Moving Data Analysis Beyond the Green Screen
reveals how to use ViewPoint, SEQUEL's graphical interface,
to convert queries into SEQUEL objects; build graphs,
tables, and reports; download data; and more.

Find out how easy data access can be--
Follow this link to learn more.


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Edwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

BCD:  FREE Webinar: New Presto 3 - The MOST flexible IBM i Web GUI for green screens. View NOW!
Vision Solutions:  Real-Time Database Sharing: What Can It Do for Your Business? FREE White Paper!
COMMON:  Join us at the 2011 Conference & Expo, May 1 - 4 in Minneapolis, MN


 

IT Jungle Store Top Book Picks

BACK IN STOCK: Easy Steps to Internet Programming for System i: List Price, $49.95

The iSeries Express Web Implementer's Guide: List Price, $49.95
The iSeries Pocket Database Guide: List Price, $59
The iSeries Pocket SQL Guide: List Price, $59
The iSeries Pocket WebFacing Primer: List Price, $39
Migrating to WebSphere Express for iSeries: List Price, $49
Getting Started with WebSphere Express for iSeries: List Price, $49
The All-Everything Operating System: List Price, $35
The Best Joomla! Tutorial Ever!: List Price, $19.95


 
The Four Hundred
That Faster Power 750 Motor Is Made for IBM i Shops

All Kinds of Goodies Added to IBM i 7.1

Power Systems Sales Jump 19 Percent in Q1

As I See It: 'He Kindly Stopped for Me'

Power Systems Get Some I/O and Storage Enhancements

Four Hundred Stuff
LANSA Turns aXes into IBM i Cloud Enabler

Centerfield Launches IBM i Auditing Tools

Japanese Quake Puts Spotlight on Supply Chain Risk

Tributary Extends NonStop Legacy to IBM i

Maxava Worked 'Just Like it Says on the Tin,' Customer Says

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

System i PTF Guide
September 25, 2010: Volume 12, Number 39

September 18, 2010: Volume 12, Number 38

September 11, 2010: Volume 12, Number 37

September 4, 2010: Volume 12, Number 36

August 28, 2010: Volume 12, Number 35

August 21, 2010: Volume 12, Number 34

TPM at The Register
HP ProLiant power supplies 'may die when dormant'

Amax moves from HPC clusters to cloud stacks

Greenpeace spies soot lining in cloud data centers

Brit outfit rolls own virtual server appliances

AMD balance sheet not exactly an Intel clone

Ubuntu Server 11.04: Fully baked in 7 days

Intel's Xeon biz bolstered by server refreshes

VMware surfs virtualization wave in Q1

Hardware boom boosts IBM in Q1

Rackspace fluffs up load balancing for clouds

Opsview beefs up Nagios system monitor

Top IT sales guy leaves HP

THIS ISSUE SPONSORED BY:

ProData Computer Services
SEQUEL Software
COMMON


Printer Friendly Version


TABLE OF CONTENTS
DDS Design with RD Power

Updating Through a Join with SQL, Take Two

Why is my i/OS Email Coming Back Undeliverable?

Four Hundred Guru

BACK ISSUES




 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2011 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement