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

86 lines
5.5 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?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="Insert values into an identity column" />
<meta name="abstract" content="You can insert a value into an identity column or allow the system to insert a value for you." />
<meta name="description" content="You can insert a value into an identity column or allow the system to insert a value for you." />
<meta name="DC.subject" content="INSERT statement, inserting identity column, identity column, inserting into, examples, inserting into identity columns" />
<meta name="keywords" content="INSERT statement, inserting identity column, identity column, inserting into, examples, inserting into identity columns" />
<meta name="DC.Relation" scheme="URI" content="rbafyinsert.htm" />
<meta name="DC.Relation" scheme="URI" content="rbafysqlpidentity.htm" />
<meta name="DC.Relation" scheme="URI" content="../db2/rbafzmstscale.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="rbafyinsertidentity" />
<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>Insert values into an identity column</title>
</head>
<body id="rbafyinsertidentity"><a name="rbafyinsertidentity"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Insert values into an identity column</h1>
<div><p>You can insert a value into an identity column or allow the system
to insert a value for you.</p>
<div class="section"><p>For example, a table has columns called ORDERNO (identity column),
SHIPPED_TO (varchar(36)), and ORDER_DATE (date). You can insert a row into
this table by issuing the following statement:</p>
<pre><strong>INSERT INTO</strong> ORDERS (SHIPPED_TO, ORDER_DATE)
<strong>VALUES</strong> ('BME TOOL', 2002-02-04)</pre>
<p>In this case, a value
is generated by the system for the identity column automatically. You can
also write this statement using the DEFAULT keyword:</p>
<pre><strong>INSERT INTO</strong> ORDERS (SHIPPED_TO, ORDER_DATE, ORDERNO)
<strong>VALUES</strong> ('BME TOOL', 2002-02-04, <strong>DEFAULT</strong>)</pre>
<p>After
the insert, you can use the IDENTITY_VAL_LOCAL function to determine the value
that the system assigned to the column. </p>
</div>
<div class="section"><p>Sometimes a value for an identity column is specified by the user,
such as in this INSERT statement using a SELECT: </p>
<pre><strong>INSERT INTO</strong> ORDERS <strong>OVERRIDING USER VALUE</strong>
(<strong>SELECT * FROM</strong> TODAYS_ORDER)</pre>
<p>In this case, OVERRIDING
USER VALUE tells the system to ignore the value provided for the identity
column from the SELECT and to generate a new value for the identity column.
OVERRIDING USER VALUE must be used if the identity column was created with
the GENERATED ALWAYS clause; it is optional for GENERATED BY DEFAULT. If OVERRIDING
USER VALUE is not specified for a GENERATED BY DEFAULT identity column, the
value provided for the column in the SELECT is inserted.</p>
</div>
<div class="section"><p>You can force the system to use the value from the select for
a GENERATED ALWAYS identity column by specifying OVERRIDING SYSTEM VALUE.
For example, issue the following statement: </p>
<pre><strong>INSERT INTO</strong> ORDERS <strong>OVERRIDING SYSTEM VALUE</strong>
(<strong>SELECT * FROM</strong> TODAYS_ORDER)</pre>
<p>This INSERT statement
uses the value from SELECT; it does not generate a new value for the identity
column. You cannot provide a value for an identity column created using GENERATED
ALWAYS without using the OVERRIDING SYSTEM VALUE clause.</p>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafyinsert.htm" title="This topic shows the basic SQL statements and clauses that insert data into tables and views. Examples using these SQL statements are supplied to help you develop SQL applications.">Insert rows using the INSERT statement</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="rbafysqlpidentity.htm" title="Every time that a new row is added to a table with an identity column, the identity column value in the new row is incremented (or decremented) by the system.">Create and alter an identity column</a></div>
</div>
<div class="relinfo"><strong>Related information</strong><br />
<div><a href="../db2/rbafzmstscale.htm">IDENTITY_VAL_LOCAL</a></div>
</div>
</div>
</body>
</html>