ibm-information-center/dist/eclipse/plugins/i5OS.ic.sqlp_5.4.0.1/rbafyexsub4.htm

78 lines
5.2 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: Correlated subqueries in an UPDATE statement" />
<meta name="abstract" content="When you use a correlated subquery in an UPDATE statement, the correlation name refers to the rows you are interested in updating." />
<meta name="description" content="When you use a correlated subquery in an UPDATE statement, the correlation name refers to the rows you are interested in updating." />
<meta name="DC.subject" content="correlation, subqueries, example UPDATE statement, subquery, correlated, example UPDATE statement, examples, correlated subquery, UPDATE statement, UPDATE statement, correlated subquery, using in" />
<meta name="keywords" content="correlation, subqueries, example UPDATE statement, subquery, correlated, example UPDATE statement, examples, correlated subquery, UPDATE statement, UPDATE statement, correlated subquery, using in" />
<meta name="DC.Relation" scheme="URI" content="rbafycorrs.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="rbafyexsub4" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: Correlated subqueries in an UPDATE statement</title>
</head>
<body id="rbafyexsub4"><a name="rbafyexsub4"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Correlated subqueries in an UPDATE statement</h1>
<div><p>When you use a correlated subquery in an UPDATE statement, the
correlation name refers to the rows you are interested in updating.</p>
<div class="section"><p>For example, when all activities of a project must be completed
before September 1983, your department considers that project to be a priority
project. You can use the SQL statement below to evaluate the projects in
the CORPDATA.PROJECT table, and write a 1 (a flag to indicate PRIORITY) in
the PRIORITY column (a column you added to CORPDATA.PROJECT for this purpose)
for each priority project. </p>
<pre> <strong>UPDATE</strong> CORPDATA.PROJECT X
<strong>SET</strong> PRIORITY = 1
<strong>WHERE</strong> '1983-09-01' &gt;
(<strong>SELECT MAX</strong>(EMENDATE)
<strong>FROM</strong> CORPDATA.EMPPROJACT
<strong>WHERE</strong> PROJNO = X.PROJNO)</pre>
</div>
<div class="section"><p>As SQL examines each row in the CORPDATA.EMPPROJACT table, it
determines the maximum activity end date (EMENDATE) for all activities of
the project (from the CORPDATA.PROJECT table). If the end date of each activity
associated with the project is before September 1983, the current row in the
CORPDATA.PROJECT table qualifies and is updated.</p>
</div>
<div class="section"><p>Update the master order table with any changes to the quantity
ordered. If the quantity in the orders table is not set (the NULL value),
keep the value that is in the master order table.</p>
<pre><strong>UPDATE</strong> MASTER_ORDERS X
<strong>SET</strong> QTY=(<strong>SELECT COALESCE</strong> (Y.QTY, X.QTY)
<strong>FROM</strong> ORDERS Y
<strong>WHERE</strong> X.ORDER_NUM = Y.ORDER_NUM)
<strong>WHERE</strong> X.ORDER_NUM <strong>IN</strong> (<strong>SELECT</strong> ORDER_NUM
<strong>FROM</strong> ORDERS)</pre>
<p>In this example, each row
of the MASTER_ORDERS table is checked to see if it has a corresponding row
in the ORDERS table. If it does have a matching row in the ORDERS table, the
COALESCE function is used to return a value for the QTY column. If QTY in
the ORDERS table has a non-null value, that value is used to update the QTY
column in the MASTER_ORDERS table. If the QTY value in the ORDERS table is
NULL, the MASTER_ORDERS QTY column is updated with its own value.</p>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafycorrs.htm" title="You can write a subquery that SQL might need to re-evaluate as it examines each new row (WHERE clause) or group of rows (HAVING clause) in the outer-level SELECT. This is called a correlated subquery.">Correlated subqueries</a></div>
</div>
</div>
</body>
</html>