doc-exports/docs/dws/dev/dws_04_0519.html
Lu, Huayi ef0ada5a59 DWS DEV 20240716 version
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Co-authored-by: Lu, Huayi <luhuayi@huawei.com>
Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
2024-11-02 09:07:47 +00:00

82 lines
10 KiB
HTML

<a name="EN-US_TOPIC_0000001510283497"></a><a name="EN-US_TOPIC_0000001510283497"></a>
<h1 class="topictitle1">GaussDB(DWS) Stored Procedure Declaration Syntax</h1>
<div id="body8662426"><div class="section" id="EN-US_TOPIC_0000001510283497__section6707173720429"><h4 class="sectiontitle">Basic Structure</h4><p id="EN-US_TOPIC_0000001510283497__a4616e19163ea42ffbcf3fc63fa7c0109">A PL/SQL block can contain a sub-block which can be placed in any section. The following describes the architecture of a PL/SQL block:</p>
<ul id="EN-US_TOPIC_0000001510283497__u9f52bce302fc418ea2ab85861c78094f"><li id="EN-US_TOPIC_0000001510283497__l552b83a25426430ab45110fcde506ff5"><strong id="EN-US_TOPIC_0000001510283497__b17524191818219">DECLARE</strong>: declares variables, types, cursors, and regional stored procedures and functions used in the PL/SQL block.<pre class="screen" id="EN-US_TOPIC_0000001510283497__s75d904a9145b4b8aaffde5c543140eb9">DECLARE</pre>
<div class="note" id="EN-US_TOPIC_0000001510283497__n96c1daca04244454a45c63e38594e52f"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="EN-US_TOPIC_0000001510283497__a694c6957b03c4afcab0a67f5ff39b571">This part is optional if no variable needs to be declared.</p>
<ul id="EN-US_TOPIC_0000001510283497__uaf1549027f8642bf871653b65feecf3c"><li id="EN-US_TOPIC_0000001510283497__lb92726ce251a4ca9a8fa8f0e3544f3bd">An anonymous block may omit the <strong id="EN-US_TOPIC_0000001510283497__b12743545192115">DECLARE</strong> keyword if no variable needs to be declared.</li><li id="EN-US_TOPIC_0000001510283497__l0d6c78694bf74304993c77d1c0d8403c">For a stored procedure, <strong id="EN-US_TOPIC_0000001510283497__b929585042110">AS</strong> is used, which is equivalent to <strong id="EN-US_TOPIC_0000001510283497__b15295750122116">DECLARE</strong>. The <strong id="EN-US_TOPIC_0000001510283497__b087015218216">AS</strong> keyword must be reserved even if there is no variable declaration part.</li></ul>
</div></div>
</li><li id="EN-US_TOPIC_0000001510283497__l57c899d8faf6435a85f55e29537de6a6"><strong id="EN-US_TOPIC_0000001510283497__b7143455192113">EXECUTION</strong>: specifies procedure and SQL statements. It is the main part of a program. It is mandatory.<pre class="screen" id="EN-US_TOPIC_0000001510283497__sa6de7035f00c4b9da6e69dfc2f833f77">BEGIN</pre>
</li><li id="EN-US_TOPIC_0000001510283497__l2cd2721038614296b3b44301f79cba8e"><strong id="EN-US_TOPIC_0000001510283497__b48661721393055">EXCEPTION</strong>: processes errors. It is optional.<pre class="screen" id="EN-US_TOPIC_0000001510283497__s9bc5c60a53d547b1b6f5b15922dd2333">EXCEPTION</pre>
</li><li id="EN-US_TOPIC_0000001510283497__lb47a74e89e204327ae3070b0b71037c7"><strong id="EN-US_TOPIC_0000001510283497__b205051506193055">END</strong><pre class="screen" id="EN-US_TOPIC_0000001510283497__s91f0cf6c66204c96918cec5a6ee72277">END;
/</pre>
<div class="notice" id="EN-US_TOPIC_0000001510283497__n24f4c41ebd164e7a983b708e1e5fb2e0"><span class="noticetitle"><img src="public_sys-resources/notice_3.0-en-us.png"> </span><div class="noticebody"><p id="EN-US_TOPIC_0000001510283497__a677c52d3b44c4c49a5f41edd9993caa9">You are not allowed to use consecutive tabs in the PL/SQL block, because they may result in an exception when the parameter <strong id="EN-US_TOPIC_0000001510283497__b1278201910225">-r</strong> is executed using the <strong id="EN-US_TOPIC_0000001510283497__b078212193224">gsql</strong> tool.</p>
</div></div>
</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001510283497__section7406195517428"><h4 class="sectiontitle">PL/SQL Block Classification</h4><p id="EN-US_TOPIC_0000001510283497__aa3ef93535f244abfb8d9a6fc0357e233">PL/SQL blocks are classified into the following types:</p>
<ul id="EN-US_TOPIC_0000001510283497__ubda7d24703f4447b8594c773ced34c2b"><li id="EN-US_TOPIC_0000001510283497__l6c522646e5bd4560ba1142034d94c9c6">Anonymous block: a dynamic block that can be executed only for once. For details about the syntax, see <a href="#EN-US_TOPIC_0000001510283497__section72513014435">Anonymous Block</a>.</li><li id="EN-US_TOPIC_0000001510283497__l555d41aa896c4ade8c83a7b3a4199a67">Subprogram: a stored procedure, function, operator, or packages stored in a database. A subprogram created in a database can be called by other programs.</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001510283497__section72513014435"><a name="EN-US_TOPIC_0000001510283497__section72513014435"></a><a name="section72513014435"></a><h4 class="sectiontitle">Anonymous Block</h4><p id="EN-US_TOPIC_0000001510283497__en-us_topic_0059779171_p18545856864">An anonymous block applies to a script infrequently executed or a one-off activity. An anonymous block is executed in a session and is not stored.</p>
<p id="EN-US_TOPIC_0000001510283497__p1606104018439"><strong id="EN-US_TOPIC_0000001510283497__b2363545104310">Syntax</strong></p>
<p id="EN-US_TOPIC_0000001510283497__a9ee665144f0242e2954ae1cc815296b4"><a href="#EN-US_TOPIC_0000001510283497__fd6784c90027648dcbeb9d3c0e164d5db">Figure 1</a> shows the syntax diagrams for an anonymous block.</p>
<div class="fignone" id="EN-US_TOPIC_0000001510283497__fd6784c90027648dcbeb9d3c0e164d5db"><a name="EN-US_TOPIC_0000001510283497__fd6784c90027648dcbeb9d3c0e164d5db"></a><a name="fd6784c90027648dcbeb9d3c0e164d5db"></a><span class="figcap"><b>Figure 1 </b>anonymous_block::=</span><br><span><img id="EN-US_TOPIC_0000001510283497__i73bd3bd0bd744a0888b4b80d47279b02" src="figure/en-us_image_0000002040138120.png" width="NaN" height="NaN"></span></div>
<p id="EN-US_TOPIC_0000001510283497__ab2b8d0464f48447794afb8ab250422bc">Details about the syntax diagram are as follows:</p>
<ul id="EN-US_TOPIC_0000001510283497__u934342a3d3db444e938a4b25e6eacbf7"><li id="EN-US_TOPIC_0000001510283497__le48b1b077c7942c086513420929da4d0">The execute part of an anonymous block starts with a <strong id="EN-US_TOPIC_0000001510283497__b63957380093055">BEGIN</strong> statement, has a break with an <strong id="EN-US_TOPIC_0000001510283497__b59657731593055">END</strong> statement, and ends with a semicolon (;). Type a slash (/) and press <strong id="EN-US_TOPIC_0000001510283497__b57035836893055">Enter</strong> to execute the statement.<div class="notice" id="EN-US_TOPIC_0000001510283497__nd0a1dd8a53324d66944035e95304b2b9"><span class="noticetitle"><img src="public_sys-resources/notice_3.0-en-us.png"> </span><div class="noticebody"><p id="EN-US_TOPIC_0000001510283497__a18d791efdc8343929dcc308de88aab87">The terminator "/" must be written in an independent row.</p>
</div></div>
</li><li id="EN-US_TOPIC_0000001510283497__le63dfc68fd854d27a58115f36eae68c5">The declaration section includes the variable definition, type, and cursor definition.</li><li id="EN-US_TOPIC_0000001510283497__lc4cefda402de482eabc10254a6d09645">A simplest anonymous block does not execute any commands. At least one statement, even a null statement, must be presented in any implementation blocks.</li></ul>
<p id="EN-US_TOPIC_0000001510283497__p5971852124419"></p>
<p id="EN-US_TOPIC_0000001510283497__p21504354411"><strong id="EN-US_TOPIC_0000001510283497__b17852201316231">Examples</strong></p>
<p id="EN-US_TOPIC_0000001510283497__a39a59a45ff7b4fc5a52e89a5175ffdbb">The following lists basic anonymous block programs:</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001510283497__s8179074fdbdb40419c5a3442dd54a2d0"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span>
<span class="normal">14</span>
<span class="normal">15</span>
<span class="normal">16</span>
<span class="normal">17</span>
<span class="normal">18</span>
<span class="normal">19</span>
<span class="normal">20</span></pre></div></td><td class="code"><div><pre><span></span><span class="c1">-- Null statement block:</span>
<span class="k">BEGIN</span>
<span class="w"> </span><span class="k">NULL</span><span class="p">;</span><span class="w"> </span>
<span class="k">END</span><span class="p">;</span>
<span class="o">/</span>
<span class="c1">-- Print information to the console:</span>
<span class="k">BEGIN</span>
<span class="w"> </span><span class="n">dbms_output</span><span class="p">.</span><span class="n">put_line</span><span class="p">(</span><span class="s1">'hello world!'</span><span class="p">);</span><span class="w"> </span>
<span class="k">END</span><span class="p">;</span><span class="w"> </span>
<span class="o">/</span>
<span class="c1">-- Print variable contents to the console:</span>
<span class="k">DECLARE</span><span class="w"> </span>
<span class="w"> </span><span class="n">my_var</span><span class="w"> </span><span class="n">VARCHAR2</span><span class="p">(</span><span class="mi">30</span><span class="p">);</span><span class="w"> </span>
<span class="k">BEGIN</span><span class="w"> </span>
<span class="w"> </span><span class="n">my_var</span><span class="w"> </span><span class="p">:</span><span class="o">=</span><span class="s1">'world'</span><span class="p">;</span><span class="w"> </span>
<span class="w"> </span><span class="n">dbms_output</span><span class="p">.</span><span class="n">put_line</span><span class="p">(</span><span class="s1">'hello'</span><span class="o">||</span><span class="n">my_var</span><span class="p">);</span><span class="w"> </span>
<span class="k">END</span><span class="p">;</span><span class="w"> </span>
<span class="o">/</span><span class="w"> </span>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="section" id="EN-US_TOPIC_0000001510283497__section12919141254512"><h4 class="sectiontitle">Subprogram</h4><p id="EN-US_TOPIC_0000001510283497__p179291413453">A subprogram stores stored procedures, functions, operators, and advanced packages. A subprogram created in a database can be called by other programs.</p>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_04_0512.html">GaussDB(DWS) Stored Procedure</a></div>
</div>
</div>