doc-exports/docs/dws/tool/dws_07_6842.html
Lu, Huayi 346ac31da9 DWS TG 8.1.3.200 VERSION
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Reviewed-by: Jiang, Beibei <beibei.jiang@t-systems.com>
Co-authored-by: Lu, Huayi <luhuayi@huawei.com>
Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
2023-08-28 09:20:17 +00:00

123 lines
10 KiB
HTML

<a name="EN-US_TOPIC_0000001234042169"></a><a name="EN-US_TOPIC_0000001234042169"></a>
<h1 class="topictitle1">DML</h1>
<div id="body8662426"><div class="section" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_section19202205568"><h4 class="sectiontitle">SELECT</h4><p id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_p19511421195618">FETCH clause</p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_table19958122025620" frame="border" border="1" rules="all"><thead align="left"><tr id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_row1851182112565"><th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.1.3.1.3.1.1"><p id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_p451621135614"><strong id="EN-US_TOPIC_0000001234042169__en-us_topic_0237712499_b1017212444475">DB2 Syntax</strong></p>
</th>
<th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.1.3.1.3.1.2"><p id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_p351192155617"><strong id="EN-US_TOPIC_0000001234042169__en-us_topic_0237712499_b81851635102616">Syntax After Migration</strong></p>
</th>
</tr>
</thead>
<tbody><tr id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_row6511212565"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.1.3.1.3.1.1 "><pre class="screen" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_screen15517211563">SELECT empno, ename, deptno
FROM emp_t
ORDER BY salary
FETCH FIRST ROW ONLY
-----
SELECT empno, ename
FROM emp_t
WHERE deptno = 10
ORDER BY salary
fetch first 2 rows only;</pre>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.1.3.1.3.1.2 "><pre class="screen" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_screen652182175613">SELECT empno, ename, deptno
FROM emp_t
ORDER BY salary
LIMIT 1;
-----
SELECT empno, ename
FROM emp_t
WHERE deptno = 10
ORDER BY salary
LIMIT 2;</pre>
</td>
</tr>
</tbody>
</table>
</div>
<div class="note" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_note3322183174613"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_p6337631104616">The fetch-first-clause sets a maximum number of rows that can be retrieved.</p>
</div></div>
</div>
<div class="section" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_section104191324195912"><h4 class="sectiontitle">WITH AS</h4><p id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_p20743202517591">WITH AS with column list</p>
</div>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_table11634144017591" frame="border" border="1" rules="all"><thead align="left"><tr id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_row14635640115914"><th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.3.1.3.1.1"><p id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_p294413384820"><strong id="EN-US_TOPIC_0000001234042169__en-us_topic_0237712499_b9635164005913">DB2 Syntax</strong></p>
</th>
<th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.3.1.3.1.2"><p id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_p19444354812"><strong id="EN-US_TOPIC_0000001234042169__en-us_topic_0237712499_b648933516308">Syntax After Migration</strong></p>
</th>
</tr>
</thead>
<tbody><tr id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_row0635140175914"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.3.1.3.1.1 "><pre class="screen" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_screen206351540205913">WITH rec (emp_no, emp_name, dept_name, dept_no) AS
( SELECT empno, ename, cast('admin' as varchar(90)), 100 AS deptno
FROM emp_t )
SELECT * FROM rec;</pre>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.3.1.3.1.2 "><pre class="screen" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_screen14636840165914">WITH rec AS
( SELECT empno AS emp_no, ename AS emp_name, cast('admin' as varchar(90)) AS dept_name, 100 AS dept_no
FROM emp_t )
SELECT * FROM rec;</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_p045111381902">WITH AS with VALUES clause</p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_table7297135312013" frame="border" border="1" rules="all"><thead align="left"><tr id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_row429718534014"><th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.5.1.3.1.1"><p id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_p23631854816"><strong id="EN-US_TOPIC_0000001234042169__en-us_topic_0237712499_b82971953302">DB2 Syntax</strong></p>
</th>
<th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.5.1.3.1.2"><p id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_p13363168104815"><strong id="EN-US_TOPIC_0000001234042169__en-us_topic_0237712499_b0907203615307">Syntax After Migration</strong></p>
</th>
</tr>
</thead>
<tbody><tr id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_row4297453202"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.5.1.3.1.1 "><pre class="screen" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_screen1329735314019"> WITH rec (baseschema, basename, baselevel) AS
( VALUES( cast('SCOTT' as varchar(90)), cast('EMP' as varchar(90)), 10000 ) )
SELECT owner, table_name, baselevel -1
FROM ALL_TABLES, REC
WHERE owner = BASESCHEMA
AND table_name = BASENAME;</pre>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.5.1.3.1.2 "><pre class="screen" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_screen42984532010"> WITH rec AS
( SELECT cast('SCOTT' as varchar(90)) AS baseschema, cast('EMP' as varchar(90)) AS basename, 10000 AS baselevel
From dual )
SELECT owner, table_name, baselevel -1
FROM ALL_TABLES, REC
WHERE owner = BASESCHEMA
AND table_name = BASENAME;</pre>
</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_section1923655014"><h4 class="sectiontitle">Table Function</h4><p id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_p6569656112">TABLE function is specified with subquery.</p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_table146679357212" frame="border" border="1" rules="all"><thead align="left"><tr id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_row2066743518210"><th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.6.3.1.3.1.1"><p id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_p1031371244818"><strong id="EN-US_TOPIC_0000001234042169__en-us_topic_0237712499_b566718359216">DB2 Syntax</strong></p>
</th>
<th align="left" class="cellrowborder" valign="top" width="50%" id="mcps1.3.6.3.1.3.1.2"><p id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_p431320124488"><strong id="EN-US_TOPIC_0000001234042169__en-us_topic_0237712499_b134251938193020">Syntax After Migration</strong></p>
</th>
</tr>
</thead>
<tbody><tr id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_row1766893517214"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.6.3.1.3.1.1 "><pre class="screen" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_screen766814351722">SELECT prod_code, prod_desc, (received_qty-issued_qty) stk
FROM prod p, TABLE(select prod_code, SUM(received_qty) received_qty FROM prod_recd) r
, TABLE(select prod_code, SUM(issued_qty) issued_qty FROM prod_issue) i
WHERE r.prod_code = p.prod_code
AND i.prod_code = p.prod_code; </pre>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.3.6.3.1.3.1.2 "><pre class="screen" id="EN-US_TOPIC_0000001234042169__en-us_topic_0238518455_en-us_topic_0237362454_en-us_topic_0213529183_screen566813512211">SELECT prod_code, prod_desc, (received_qty-issued_qty) stk
FROM prod p, (select prod_code, SUM(received_qty) received_qty FROM prod_recd) r
, (select prod_code, SUM(issued_qty) issued_qty FROM prod_issue) i
WHERE r.prod_code = p.prod_code
AND i.prod_code = p.prod_code;</pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_07_0684.html">DB2 Syntax Migration</a></div>
</div>
</div>