doc-exports/docs/dws/dev/dws_04_0972.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

33 lines
15 KiB
HTML

<a name="EN-US_TOPIC_0000001188323666"></a><a name="EN-US_TOPIC_0000001188323666"></a>
<h1 class="topictitle1">PL/pgSQL Usage</h1>
<div id="body0000001229098763"><div class="section" id="EN-US_TOPIC_0000001188323666__section1823123962218"><h4 class="sectiontitle">General Principles</h4><ol id="EN-US_TOPIC_0000001188323666__ol137561116204615"><li id="EN-US_TOPIC_0000001188323666__li526781634619">Development shall strictly comply with design documents.</li><li id="EN-US_TOPIC_0000001188323666__li17375112864613">Program modules shall be highly cohesive and loosely coupled.</li><li id="EN-US_TOPIC_0000001188323666__li149991046154615">Proper, comprehensive troubleshooting measures shall be developed.</li><li id="EN-US_TOPIC_0000001188323666__li125761413477">Code shall be reasonable and clear.</li><li id="EN-US_TOPIC_0000001188323666__li63291917184714">Program names shall comply with a unified naming rule.</li><li id="EN-US_TOPIC_0000001188323666__li1813473894718">Fully consider the program efficiency, including the program execution efficiency and database query and storage efficiency. Use efficient and effective processing methods.</li><li id="EN-US_TOPIC_0000001188323666__li6184336194817">Program comments shall be detailed, correct, and standard.</li><li id="EN-US_TOPIC_0000001188323666__li25183112505">The <strong id="EN-US_TOPIC_0000001188323666__b947965714216">COMMIT</strong> or <strong id="EN-US_TOPIC_0000001188323666__b0387185415220">ROLLBACK</strong> operation shall be performed at the end of a stored procedure, unless otherwise required by applications.</li><li id="EN-US_TOPIC_0000001188323666__li7109759155111">Programs shall support 24/7 processing. In the case of an interruption, the applications shall provide secure, easy-to-use resuming features.</li><li id="EN-US_TOPIC_0000001188323666__li1011451155213">Application output shall be standard and simple. The output shall show the progress, error description, and execution results for application maintenance personnel, and provide clear and intuitive reports and documents for business personnel.</li></ol>
</div>
<div class="section" id="EN-US_TOPIC_0000001188323666__section11688203842317"><h4 class="sectiontitle">Programming Principles</h4><ol id="EN-US_TOPIC_0000001188323666__ol19233141714588"><li id="EN-US_TOPIC_0000001188323666__li14233917155817">Use bound variables in SQL statements in the PL/pgSQL.</li><li id="EN-US_TOPIC_0000001188323666__li11321261037"><strong id="EN-US_TOPIC_0000001188323666__b122865542277">RETURNING</strong> is recommended for SQL statements in PL/pgSQL.</li><li id="EN-US_TOPIC_0000001188323666__li2629531739">Principles for using stored procedures:<ol type="a" id="EN-US_TOPIC_0000001188323666__ol199971016765"><li id="EN-US_TOPIC_0000001188323666__li16774416366">Do not use more than 50 output parameters of the Varchar or Varchar2 type in a stored procedure.</li><li id="EN-US_TOPIC_0000001188323666__li1557321781211">Do not use the LONG type for input or output parameters.</li><li id="EN-US_TOPIC_0000001188323666__li1138710412176">Use the CLOB type for output strings that exceed 10 MB.</li></ol>
</li><li id="EN-US_TOPIC_0000001188323666__li132847596172">Variable declaration principles:<ol type="a" id="EN-US_TOPIC_0000001188323666__ol10755159121718"><li id="EN-US_TOPIC_0000001188323666__li1497583771813">Use <strong id="EN-US_TOPIC_0000001188323666__b031804012283">%TYPE</strong> to declare a variable that has the same meaning as that of a column or variable in an application table.</li><li id="EN-US_TOPIC_0000001188323666__li2018432211">Use <strong id="EN-US_TOPIC_0000001188323666__b5248205212812">%ROWTYPE</strong> to declare a record that has the same meaning as that of a row in an application table.</li><li id="EN-US_TOPIC_0000001188323666__li0865155142113">Each line of a variable declaration shall contain only one statement.</li><li id="EN-US_TOPIC_0000001188323666__li1619122111220">Do not declare variables of the LONG type.</li></ol>
</li><li id="EN-US_TOPIC_0000001188323666__li10647284220">Principles for using cursors:<ol type="a" id="EN-US_TOPIC_0000001188323666__ol148419288225"><li id="EN-US_TOPIC_0000001188323666__li33165012210">Explicit cursors shall be closed after being used.</li><li id="EN-US_TOPIC_0000001188323666__li19304155152314">Cursor variables must be closed after being used. If a cursor variable needs to transfer data to the invoked application, close the cursor in the application. If a cursor variable is used only in a stored procedure, close the cursor explicitly.</li><li id="EN-US_TOPIC_0000001188323666__li716222372218">Before using <strong id="EN-US_TOPIC_0000001188323666__b4018233323">DBMS_SQL.CLOSE_CURSOR</strong> to close a cursor, use <strong id="EN-US_TOPIC_0000001188323666__b562426183217">DBMS_SQL.IS_OPEN</strong> to check whether the cursor is open.</li></ol>
</li><li id="EN-US_TOPIC_0000001188323666__li19377554193313">Principles for collections: You are advised to use the FOR ALL statement instead of the FOR loop statement to reference elements in a collection.</li><li id="EN-US_TOPIC_0000001188323666__li1828082819359">Principles for using dynamic statements:<ol type="a" id="EN-US_TOPIC_0000001188323666__ol7864103473511"><li id="EN-US_TOPIC_0000001188323666__li1764043418355">Dynamic SQL shall not be used in the transaction programs of online systems.</li><li id="EN-US_TOPIC_0000001188323666__li128510017368">Dynamic SQL statements can be used to implement DDL statements and system control commands in PL/pgSQL.</li><li id="EN-US_TOPIC_0000001188323666__li1558819133720">Variable binding is recommended.</li></ol>
</li><li id="EN-US_TOPIC_0000001188323666__li1971198164011">Principles for assembling SQL statements:<ol type="a" id="EN-US_TOPIC_0000001188323666__ol1416711817403"><li id="EN-US_TOPIC_0000001188323666__li158741417154017">You are advised to use bound variables to assemble SQL statements.</li><li id="EN-US_TOPIC_0000001188323666__li613363754010">If the conditions for assembling SQL statements contain external input sources, the characters in the input conditions shall be checked to prevent attacks.</li><li id="EN-US_TOPIC_0000001188323666__li1927518154115">In a PL/pgSQL script, the length of a single line of code cannot exceed 2499 characters.</li></ol>
</li><li id="EN-US_TOPIC_0000001188323666__li10852729194218">Principles for using triggers:<ol type="a" id="EN-US_TOPIC_0000001188323666__ol1453210384426"><li id="EN-US_TOPIC_0000001188323666__li225510386421">Triggers can be used to implement availability design in scenarios where differential data logs are irrelevant to service processing.</li><li id="EN-US_TOPIC_0000001188323666__li1994572934310">Do not use triggers to implement service processing functions.</li></ol>
</li></ol>
</div>
<div class="section" id="EN-US_TOPIC_0000001188323666__section18348046192416"><h4 class="sectiontitle">Exception Handling Principles</h4><p id="EN-US_TOPIC_0000001188323666__p2502131717257">Any error that occurs in a PL/pgSQL function aborts the execution of the function and related transactions. You can use a <strong id="EN-US_TOPIC_0000001188323666__b1873343344017">BEGIN</strong> block with an <strong id="EN-US_TOPIC_0000001188323666__b15357173674010">EXCEPTION</strong> clause to catch and fix errors.</p>
<ol id="EN-US_TOPIC_0000001188323666__ol41986525417"><li id="EN-US_TOPIC_0000001188323666__li2198652546">In a PL/pgSQL block, if an SQL statement cannot return a definite result, you are advised to handle exceptions (if any) in <strong id="EN-US_TOPIC_0000001188323666__b15761182544318">EXCEPTION</strong>. Otherwise, unhandled errors may be transferred to the external block and cause program logic errors.</li><li id="EN-US_TOPIC_0000001188323666__li131111363310">You can directly use the exceptions that have been defined in the system. DWS does not support custom exceptions.</li><li id="EN-US_TOPIC_0000001188323666__li19115154314249">A block containing an <strong id="EN-US_TOPIC_0000001188323666__en-us_topic_0058965745_b84235270621156">EXCEPTION</strong> clause is more expensive to enter and exit than a block without one. Therefore, do not use <strong id="EN-US_TOPIC_0000001188323666__b410281444711">EXCEPTION</strong> without need.</li></ol>
</div>
<div class="section" id="EN-US_TOPIC_0000001188323666__section820413810237"><h4 class="sectiontitle">Writing Standard</h4><ol id="EN-US_TOPIC_0000001188323666__ol07331119168"><li id="EN-US_TOPIC_0000001188323666__li117411171611">Variable naming rules:<ol type="a" id="EN-US_TOPIC_0000001188323666__ol325512189162"><li id="EN-US_TOPIC_0000001188323666__li121181819165">The input parameter format of a procedure or function is <strong id="EN-US_TOPIC_0000001188323666__b16245143817493">IN_</strong><em id="EN-US_TOPIC_0000001188323666__i912164817492">Parameter_name</em>. The parameter name shall be in uppercase.</li><li id="EN-US_TOPIC_0000001188323666__li1076618911216">The output parameter format of a procedure or function is <strong id="EN-US_TOPIC_0000001188323666__b7484142385319">OUT_</strong><em id="EN-US_TOPIC_0000001188323666__i15484823185311">Parameter_name</em>. The parameter name shall be in uppercase.</li><li id="EN-US_TOPIC_0000001188323666__li12569155210214">The input and output parameter format of a procedure or function is <strong id="EN-US_TOPIC_0000001188323666__b571094345318">IO_</strong><em id="EN-US_TOPIC_0000001188323666__i1071611437536">Parameter_name</em>. The parameter name shall be in uppercase.</li><li id="EN-US_TOPIC_0000001188323666__li4546131862311">Variables used in procedures and functions shall be composed of <strong id="EN-US_TOPIC_0000001188323666__b999710133555">v_</strong><em id="EN-US_TOPIC_0000001188323666__i81411524185514">Variable_name</em>. The variable name shall be in lower case.</li><li id="EN-US_TOPIC_0000001188323666__li199502782418">In query concatenation, the concatenation variable name of the <strong id="EN-US_TOPIC_0000001188323666__b1658483085914">WHERE</strong> statement shall be <strong id="EN-US_TOPIC_0000001188323666__b114581036175910">v_where</strong>, and the concatenation variable name of the <strong id="EN-US_TOPIC_0000001188323666__b1148074055914">SELECT</strong> statement shall be <strong id="EN-US_TOPIC_0000001188323666__b126246195911">v_select</strong>.</li><li id="EN-US_TOPIC_0000001188323666__li261712019272">The record type (TYPE) name shall consist of <strong id="EN-US_TOPIC_0000001188323666__b1335984519412">T</strong> and a variable name. The name shall be in uppercase.</li><li id="EN-US_TOPIC_0000001188323666__li1082414562272">A cursor name shall consist of <strong id="EN-US_TOPIC_0000001188323666__b137289455315">CUR</strong> and a variable name. The name shall be in uppercase.</li><li id="EN-US_TOPIC_0000001188323666__li186271728172911">The name of a reference cursor (REF CURSOR) shall consist of <strong id="EN-US_TOPIC_0000001188323666__b17514108268">REF</strong> and a variable name. The name shall be in uppercase.</li></ol>
</li><li id="EN-US_TOPIC_0000001188323666__li736762718408">Rules for defining variable types:<ol type="a" id="EN-US_TOPIC_0000001188323666__ol72412396408"><li id="EN-US_TOPIC_0000001188323666__li18629153814403">Use <strong id="EN-US_TOPIC_0000001188323666__b79962253711">%TYPE</strong> to declare the type of a variable that has the same meaning as that of a column in an application table.</li><li id="EN-US_TOPIC_0000001188323666__li1717762424112">Use <strong id="EN-US_TOPIC_0000001188323666__b127588551672">%ROWTYPE</strong> to declare the type of a record that has the same meaning as that of a row in an application table.</li></ol>
</li><li id="EN-US_TOPIC_0000001188323666__li1785672914446">Rules for writing comments:<ol type="a" id="EN-US_TOPIC_0000001188323666__ol1268093914417"><li id="EN-US_TOPIC_0000001188323666__li194081639184416">Comments shall be meaningful and shall not just repeat the code content.</li><li id="EN-US_TOPIC_0000001188323666__li178375815447">Comments shall be concise and easy to understand. </li><li id="EN-US_TOPIC_0000001188323666__li18925193814458">Comments shall be provided at the beginning of each stored procedure or function. The comments shall contain a brief function description, author, compilation date, program version number, and program change history. The format of the comments at the beginning of stored procedures shall be the same.</li><li id="EN-US_TOPIC_0000001188323666__li163491273472">Comments shall be provided next to the input and output parameters to describe the meaning of variables.</li><li id="EN-US_TOPIC_0000001188323666__li53209524472">Comments shall be provided at the beginning of each block or large branch to briefly describe the function of the block. If an algorithm is used, comments shall be provided to describe the purpose and result of the algorithm.</li></ol>
</li><li id="EN-US_TOPIC_0000001188323666__li14643145024819">Variable declaration format:<p id="EN-US_TOPIC_0000001188323666__p9458165014"><a name="EN-US_TOPIC_0000001188323666__li14643145024819"></a><a name="li14643145024819"></a>Each line shall contain only one statement. To assign initial values, write them in the same line.</p>
</li><li id="EN-US_TOPIC_0000001188323666__li1519858184917">Letter case:<p id="EN-US_TOPIC_0000001188323666__p3191181165011"><a name="EN-US_TOPIC_0000001188323666__li1519858184917"></a><a name="li1519858184917"></a>Use uppercase letters except for variable names.</p>
</li><li id="EN-US_TOPIC_0000001188323666__li131901726155016">Indentation:<p id="EN-US_TOPIC_0000001188323666__p2662153445019"><a name="EN-US_TOPIC_0000001188323666__li131901726155016"></a><a name="li131901726155016"></a>In the statements used for creating a stored procedure, the keywords <strong id="EN-US_TOPIC_0000001188323666__b744953481718">CREATE</strong>, <strong id="EN-US_TOPIC_0000001188323666__b119264013172">AS/IS</strong>, <strong id="EN-US_TOPIC_0000001188323666__b127491344191714">BEGIN</strong>, and <strong id="EN-US_TOPIC_0000001188323666__b1947734781710">END</strong> at the same level shall have the same indent.</p>
</li><li id="EN-US_TOPIC_0000001188323666__li169608420512">Statement rules:<ol type="a" id="EN-US_TOPIC_0000001188323666__ol20775948185110"><li id="EN-US_TOPIC_0000001188323666__li15399124814515">For statements that define variables, Each line shall contain only one statement.</li><li id="EN-US_TOPIC_0000001188323666__li74811342521">The keywords <strong id="EN-US_TOPIC_0000001188323666__b419124719294">IF</strong>, <strong id="EN-US_TOPIC_0000001188323666__b179975132910">ELSE IF</strong>, <strong id="EN-US_TOPIC_0000001188323666__b13271553132913">ELSE</strong>, and <strong id="EN-US_TOPIC_0000001188323666__b9139175582913">END</strong> at the same level shall have the same indent.</li><li id="EN-US_TOPIC_0000001188323666__li9898183385211">The keywords <strong id="EN-US_TOPIC_0000001188323666__b66369983015">CASE</strong> and <strong id="EN-US_TOPIC_0000001188323666__b1171018111303">END</strong> shall have the same indent. The keywords <strong id="EN-US_TOPIC_0000001188323666__b1421916221301">WHEN</strong> and <strong id="EN-US_TOPIC_0000001188323666__b21174248305">ELSE</strong> shall be indented.</li><li id="EN-US_TOPIC_0000001188323666__li1169575675210">The keywords <strong id="EN-US_TOPIC_0000001188323666__b3437546203013">LOOP</strong> and <strong id="EN-US_TOPIC_0000001188323666__b37241480303">END LOOP</strong> at the same level shall have the same indent. Nested statements or statements at lower levels shall have more indent.</li></ol>
</li></ol>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_04_0074.html">Development and Design Proposal</a></div>
</div>
</div>