doc-exports/docs/dws/dev/dws_04_0558.html
Lu, Huayi e6fa411af0 DWS DEV 830.201 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-05-16 07:24:04 +00:00

111 lines
17 KiB
HTML

<a name="EN-US_TOPIC_0000001188323602"></a><a name="EN-US_TOPIC_0000001188323602"></a>
<h1 class="topictitle1">Debugging</h1>
<div id="body8662426"><div class="section" id="EN-US_TOPIC_0000001188323602__s3c8992fd91b94b1182ac4297783233b3"><h4 class="sectiontitle">Syntax</h4><p id="EN-US_TOPIC_0000001188323602__abc6cb87b9f9544e0a08f3f81631e3863">RAISE has the following five syntax formats:</p>
<div class="fignone" id="EN-US_TOPIC_0000001188323602__f74b77b864d8441cf917ab406447fe473"><span class="figcap"><b>Figure 1 </b>raise_format::=</span><br><span><img id="EN-US_TOPIC_0000001188323602__i72768f22d8bf454abd4783d9499e67fa" src="figure/en-us_image_0000001188163798.png"></span></div>
<div class="fignone" id="EN-US_TOPIC_0000001188323602__fd13f955eeb294a1eb21ddb0f0c59aac1"><span class="figcap"><b>Figure 2 </b>raise_condition::=</span><br><span><img id="EN-US_TOPIC_0000001188323602__i4579a1694920486591481b8c4b830df9" src="figure/en-us_image_0000001233761915.png"></span></div>
<div class="fignone" id="EN-US_TOPIC_0000001188323602__f46048fc21af1443988e3794ad2f58bf9"><span class="figcap"><b>Figure 3 </b>raise_sqlstate::=</span><br><span><img id="EN-US_TOPIC_0000001188323602__id9b5fb6d400d4d5f9c6dd9efadb8feb4" src="figure/en-us_image_0000001188482328.png"></span></div>
<div class="fignone" id="EN-US_TOPIC_0000001188323602__f2194d9f82d0b4c138d2b64d7207f02b0"><span class="figcap"><b>Figure 4 </b>raise_option::=</span><br><span><img id="EN-US_TOPIC_0000001188323602__ie1ce7fcc0ae74caf9e9327d76d83d7ae" src="figure/en-us_image_0000001233681843.png"></span></div>
<div class="fignone" id="EN-US_TOPIC_0000001188323602__f83a9e69cf9474bd5a885130e5c140da8"><a name="EN-US_TOPIC_0000001188323602__f83a9e69cf9474bd5a885130e5c140da8"></a><a name="f83a9e69cf9474bd5a885130e5c140da8"></a><span class="figcap"><b>Figure 5 </b>raise::=</span><br><span><img id="EN-US_TOPIC_0000001188323602__i0de8e5076a924fca952bb78c117e4ac2" src="figure/en-us_image_0000001233563351.png"></span></div>
<p id="EN-US_TOPIC_0000001188323602__a5460628306da475cab7a35e7ec7bc39e">Parameter description:</p>
<ul id="EN-US_TOPIC_0000001188323602__ue3dafe846b3a4a18adc87ff593314458"><li id="EN-US_TOPIC_0000001188323602__l899abf5769be4036b50f680d11fcacba">The level option is used to specify the error level, that is, <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b6555104217159">DEBUG</strong>, <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b5308847417159">LOG</strong>, <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b803421917159">INFO</strong>, <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b519911217159">NOTICE</strong>, <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b4679200817159">WARNING</strong>, or <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b1847489417159">EXCEPTION</strong> (default). <strong id="EN-US_TOPIC_0000001188323602__b113701664041511">EXCEPTION</strong> throws an error that normally terminates the current transaction and the others only generate information at their levels. The <a href="dws_04_0915.html#EN-US_TOPIC_0000001233883395__s1ffb0797361d413d875381200fed970b">log_min_messages</a> and <a href="dws_04_0915.html#EN-US_TOPIC_0000001233883395__sbd8ad9bb6b9b48ba97f998f060dc56f3">client_min_messages</a> parameters control whether the error messages of specific levels are reported to the client and are written to the server log.</li><li id="EN-US_TOPIC_0000001188323602__lc227af9a2d6948749fee41abea77d997"><strong id="EN-US_TOPIC_0000001188323602__b143909285241511">format</strong>: specifies the error message text to be reported, a format character string. The format character string can be appended with an expression for insertion to the message text. In a format character string, <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b2116205217159">%</strong> is replaced by the parameter value attached to format and <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b5624074617159">%%</strong> is used to print <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b3640466917159">%</strong>. For example:<pre class="screen" id="EN-US_TOPIC_0000001188323602__scf7d0fc329444799be15c8626ea33d96">--v_job_id replaces % in the character string.
RAISE NOTICE 'Calling cs_create_job(%)',v_job_id;</pre>
</li><li id="EN-US_TOPIC_0000001188323602__leaf398b2b7a943e682a754ab1655217f">option = expression: inserts additional information to an error report. The keyword option can be <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b44987517159">MESSAGE</strong>, <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b404888317159">DETAIL</strong>, <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b3643995317159">HINT</strong>, or <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b5952412617159">ERRCODE</strong>, and each expression can be any character string.<ul id="EN-US_TOPIC_0000001188323602__u50148cb4067d491688dbbf3c36524cae"><li id="EN-US_TOPIC_0000001188323602__lcc45c0f57c3741b0b3559c96c4817717">MESSAGE: specifies the error message text. This option cannot be used in a RAISE statement that contains a format character string in front of USING.</li><li id="EN-US_TOPIC_0000001188323602__lbc6d96a1c38d462b9f836a9733696f41">DETAIL: specifies detailed information of an error.</li><li id="EN-US_TOPIC_0000001188323602__l256f760e429d4147a7aacc1768ef1615">HINT: prints hint information.</li><li id="EN-US_TOPIC_0000001188323602__labdf83a580b54b00982325108728c791"><strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b3682313017159">ERRCODE</strong>: designates an error code (SQLSTATE) to a report. A condition name or a five-character SQLSTATE error code can be used.</li></ul>
</li><li id="EN-US_TOPIC_0000001188323602__l7aea02653dc64e9197cfab2d745aeb69">condition_name: specifies the condition name corresponding to the error code.</li><li id="EN-US_TOPIC_0000001188323602__lbffd859f93914eccb838a9aeeda2d3ba">sqlstate: specifies the error code.</li></ul>
<p id="EN-US_TOPIC_0000001188323602__af54b043dc60949ea8b5de69b32e1d05c">If neither a condition name nor an <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b1467256917159">SQLSTATE</strong> is designated in a <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b6494426317159">RAISE EXCEPTION</strong> command, the <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b4762745617159">RAISE EXCEPTION (P0001)</strong> is used by default. If no message text is designated, the condition name or SQLSTATE is used as the message text by default.</p>
<div class="notice" id="EN-US_TOPIC_0000001188323602__n43d438a77956480cba31a1bf67688c74"><span class="noticetitle"><img src="public_sys-resources/notice_3.0-en-us.png"> </span><div class="noticebody"><p id="EN-US_TOPIC_0000001188323602__ae5725f5e97c04d1abc5c3f5f2a3469f1">If the <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b2403293117331">SQLSTATE</strong> designates an error code, the error code is not limited to a defined error code. It can be any error code containing five digits or ASCII uppercase rather than <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b1496979517331">00000</strong>. Do not use an error code ended with three zeros because this kind of error codes are type codes and can be captured by the whole category.</p>
</div></div>
<div class="note" id="EN-US_TOPIC_0000001188323602__n675404cbecb24e588147738e1ec9a094"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="EN-US_TOPIC_0000001188323602__aafee45ed23d2444384350a26877b62ab">The syntax described in <a href="#EN-US_TOPIC_0000001188323602__f83a9e69cf9474bd5a885130e5c140da8">Figure 5</a> does not append any parameter. This form is used only for the <strong id="EN-US_TOPIC_0000001188323602__b137969564241511">EXCEPTION</strong> statement in a <strong id="EN-US_TOPIC_0000001188323602__b143385159641511">BEGIN</strong> block so that the error can be re-processed.</p>
</div></div>
</div>
<div class="section" id="EN-US_TOPIC_0000001188323602__s30dc510dbb944ef1a50dee80376cea60"><h4 class="sectiontitle">Examples</h4><div class="p" id="EN-US_TOPIC_0000001188323602__a047bc1557772402496979d3122f7ecf9">Display error and hint information when a transaction terminates:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188323602__s32b8cdc24a884ff09f5d2f3afdf7bc57"><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></pre></div></td><td class="code"><div><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">OR</span><span class="w"> </span><span class="k">REPLACE</span><span class="w"> </span><span class="k">PROCEDURE</span><span class="w"> </span><span class="n">proc_raise1</span><span class="p">(</span><span class="n">user_id</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="nb">integer</span><span class="p">)</span>
<span class="k">AS</span><span class="w"> </span>
<span class="k">BEGIN</span><span class="w"> </span>
<span class="n">RAISE</span><span class="w"> </span><span class="k">EXCEPTION</span><span class="w"> </span><span class="s1">'Noexistence ID --&gt; %'</span><span class="p">,</span><span class="n">user_id</span><span class="w"> </span><span class="k">USING</span><span class="w"> </span><span class="n">HINT</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'Please check your user ID'</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>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188323602__screen102311911447"><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></pre></div></td><td class="code"><div><pre><span></span><span class="k">CALL</span><span class="w"> </span><span class="n">proc_raise1</span><span class="p">(</span><span class="mi">300011</span><span class="p">);</span>
<span class="n">ERROR</span><span class="p">:</span><span class="w"> </span><span class="n">Noexistence</span><span class="w"> </span><span class="n">ID</span><span class="w"> </span><span class="c1">--&gt; 300011</span>
<span class="n">HINT</span><span class="p">:</span><span class="w"> </span><span class="n">Please</span><span class="w"> </span><span class="k">check</span><span class="w"> </span><span class="n">your</span><span class="w"> </span><span class="k">user</span><span class="w"> </span><span class="n">ID</span>
</pre></div></td></tr></table></div>
</div>
<div class="p" id="EN-US_TOPIC_0000001188323602__aa30fea56227d45619befb8a04399bc12">Two methods are available for setting <strong id="EN-US_TOPIC_0000001188323602__en-us_topic_0027043000_b4637828017412">SQLSTATE</strong>:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188323602__sb0a6701280424d09a9310e1e6f8dbd80"><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></pre></div></td><td class="code"><div><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">OR</span><span class="w"> </span><span class="k">REPLACE</span><span class="w"> </span><span class="k">PROCEDURE</span><span class="w"> </span><span class="n">proc_raise2</span><span class="p">(</span><span class="n">user_id</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="nb">integer</span><span class="p">)</span>
<span class="k">AS</span><span class="w"> </span>
<span class="k">BEGIN</span><span class="w"> </span>
<span class="n">RAISE</span><span class="w"> </span><span class="s1">'Duplicate user ID: %'</span><span class="p">,</span><span class="n">user_id</span><span class="w"> </span><span class="k">USING</span><span class="w"> </span><span class="n">ERRCODE</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'unique_violation'</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>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188323602__screen0383101354717"><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></pre></div></td><td class="code"><div><pre><span></span><span class="err">\</span><span class="k">set</span><span class="w"> </span><span class="n">VERBOSITY</span><span class="w"> </span><span class="k">verbose</span>
<span class="k">CALL</span><span class="w"> </span><span class="n">proc_raise2</span><span class="p">(</span><span class="mi">300011</span><span class="p">);</span>
<span class="n">ERROR</span><span class="p">:</span><span class="w"> </span><span class="n">Duplicate</span><span class="w"> </span><span class="k">user</span><span class="w"> </span><span class="n">ID</span><span class="p">:</span><span class="w"> </span><span class="mi">300011</span>
<span class="k">SQLSTATE</span><span class="p">:</span><span class="w"> </span><span class="mi">23505</span>
<span class="k">LOCATION</span><span class="p">:</span><span class="w"> </span><span class="n">exec_stmt_raise</span><span class="p">,</span><span class="w"> </span><span class="n">pl_exec</span><span class="p">.</span><span class="n">cpp</span><span class="p">:</span><span class="mi">3482</span>
</pre></div></td></tr></table></div>
</div>
<p id="EN-US_TOPIC_0000001188323602__ab5e06d69b5424f2cb054a148df77da89">If the main parameter is a condition name or <strong id="EN-US_TOPIC_0000001188323602__b13349155393211">SQLSTATE</strong>, the following applies:</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188323602__screen8884203610134"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span></pre></div></td><td class="code"><div><pre><span></span><span class="n">RAISE</span><span class="w"> </span><span class="n">division_by_zero</span><span class="p">;</span>
<span class="n">RAISE</span><span class="w"> </span><span class="k">SQLSTATE</span><span class="w"> </span><span class="s1">'22012'</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
<p id="EN-US_TOPIC_0000001188323602__a0413ed2d990c4167b745c22b8ad19008">For example:</p>
<pre class="screen" id="EN-US_TOPIC_0000001188323602__s1010900900d84d63b92a3cf5598c1b46">CREATE OR REPLACE PROCEDURE division(div in integer, dividend in integer)
AS
DECLARE
res int;
BEGIN
IF dividend=0 THEN
RAISE division_by_zero;
RETURN;
ELSE
res := div/dividend;
RAISE INFO 'division result: %', res;
RETURN;
END IF;
END;
/</pre>
<pre class="screen" id="EN-US_TOPIC_0000001188323602__screen134931339136">call division(3,0);
ERROR: division_by_zero</pre>
<div class="p" id="EN-US_TOPIC_0000001188323602__a83f6d11f70c6421b9e6bf5dcad9d1694">Alternatively:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188323602__s975475c3829c4ca3ba9c2a834f8f62e8"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="n">RAISE</span><span class="w"> </span><span class="n">unique_violation</span><span class="w"> </span><span class="k">USING</span><span class="w"> </span><span class="n">MESSAGE</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'Duplicate user ID: '</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">user_id</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_04_0512.html">Stored Procedures</a></div>
</div>
</div>