doc-exports/docs/ddm/umn/ddm_03_0037.html
Hasko, Vladimir a5cc543164 reverting ddm umn to state before last commit in #613
Reviewed-by: Wagner, Fabian <fabian.wagner@t-systems.com>
Co-authored-by: Hasko, Vladimir <vladimir.hasko@t-systems.com>
Co-committed-by: Hasko, Vladimir <vladimir.hasko@t-systems.com>
2024-01-17 13:06:49 +00:00

30 lines
4.8 KiB
HTML

<a name="ddm_03_0037"></a><a name="ddm_03_0037"></a>
<h1 class="topictitle1">Using Global Sequences in INSERT or REPLACE Statements</h1>
<div id="body0000001073670011"><p id="ddm_03_0037__p661745122611">You can use global sequences in INSERT or REPLACE statements to provide unique global sequence across schemas in a DDM instance. Generating sequence numbers with NEXTVAL and CURRVAL is supported in INSERT or REPLACE statements. For example, you can execute schema.seq.nextval and schema.seq.currval to obtain global sequence numbers. CURRVAL returns the current sequence number, and NEXTVAL returns the next one. If no schema is specified, use the global sequence of the currently connected schema.</p>
<p id="ddm_03_0037__p193917485269">Concurrently executing schema.seq.nextval in multiple sessions is supported to obtain unique global sequence numbers.</p>
<div class="section" id="ddm_03_0037__section1759131915615"><h4 class="sectiontitle">Prerequisites</h4><ul id="ddm_03_0037__ul325685314442"><li id="ddm_03_0037__li1625617533442">There are two schemas <strong id="ddm_03_0037__b583125081112">dml_test_1</strong> and <strong id="ddm_03_0037__b18799556191119">dml_test_2</strong>.</li><li id="ddm_03_0037__li1025685314418">Both of them have table <strong id="ddm_03_0037__b3641741181217">test_seq</strong>.<p id="ddm_03_0037__p4151520202720">Run the following command to create a table:</p>
<p id="ddm_03_0037__p13286151184417"><strong id="ddm_03_0037__b312140192719">create table test_seq(col1 bigint,col2 bigint) dbpartition by hash(col1);</strong></p>
</li></ul>
</div>
<div class="section" id="ddm_03_0037__section19371751145511"><h4 class="sectiontitle">Procedure</h4><ol id="ddm_03_0037__ol186265616550"><li id="ddm_03_0037__li286215615519"><span>Log in to the required DDM instance using a client.</span></li><li id="ddm_03_0037__li13862105635511"><span>Click the <strong id="ddm_03_0037__b950617211162">dml_test_1</strong> schema and run the following commands to create a global sequence:</span><p><p id="ddm_03_0037__p694231613214"><strong id="ddm_03_0037__b918816231423">use dml_test_1</strong>;</p>
<p id="ddm_03_0037__p1999045823"><strong id="ddm_03_0037__b1121435131713">create sequence seq_test</strong>;</p>
<p id="ddm_03_0037__p899065724"><span><img id="ddm_03_0037__image119901656218" src="en-us_image_0000001733146257.png"></span></p>
</p></li><li id="ddm_03_0037__li2863175625516"><span>Run the following command to use the global sequence in an INSERT or REPLACE statement:</span><p><p id="ddm_03_0037__p13866265117"><strong id="ddm_03_0037__b49024539113">insert into test_seq(col1,col2)values(seq_test.nextval,seq_test.currval)</strong>;</p>
<p id="ddm_03_0037__p88140168115"><span><img id="ddm_03_0037__image1381441619118" src="en-us_image_0000001685147446.png"></span></p>
</p></li><li id="ddm_03_0037__li116155820494"><span>Click the <strong id="ddm_03_0037__b157781740171614">dml_test_2</strong> schema, run the following commands to use the global sequence in an INSERT or REPLACE statement:</span><p><p id="ddm_03_0037__p10585628185012"><strong id="ddm_03_0037__b11817192139">use dml_test_2</strong>;</p>
<p id="ddm_03_0037__p1753635611220"><strong id="ddm_03_0037__b42311711311">insert into test_seq(col1,col2)values(dml_test_1.seq_test.nextval,dml_test_1.seq_test.currval)</strong>;</p>
<p id="ddm_03_0037__p1790311462311"><span><img id="ddm_03_0037__image89034461033" src="en-us_image_0000001685307194.png"></span></p>
<p id="ddm_03_0037__p06155513499">The global sequence is created in schema <strong id="ddm_03_0037__b999603442215">dml_test_1</strong>. To use the global sequence in <strong id="ddm_03_0037__b12934194732320">schema dml_test_2</strong>, you need to specify a schema name, for example, <strong id="ddm_03_0037__b16678640113420">dml_test_1.seq_test.nextval</strong> or <strong id="ddm_03_0037__b165089446346">dml_test_1.seq_test.currval</strong>.</p>
<div class="note" id="ddm_03_0037__note18615514490"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><ul id="ddm_03_0037__ul1695594912"><li id="ddm_03_0037__li5695514492">Using global sequences in INSERT and REPLACE statements is supported only in sharded tables, but not in broadcast or unsharded tables.</li><li id="ddm_03_0037__li9635574912">NEXTVAL and CURRVAL are executed from left to right in INSERT and REPLACE statements. If NEXTVAL is referenced more than once in a single statement, the sequence number is incremented for each reference.</li><li id="ddm_03_0037__li56165517491">Each global sequence belongs to a schema. When you delete a schema, the global sequence of the schema is also deleted.</li></ul>
</div></div>
</p></li></ol>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="ddm_03_0030.html">Global Sequence</a></div>
</div>
</div>