Editing
COBOL Std 6 Embedded SQL Standards GixSQL EXEC SQL
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== 6. Embedded SQL Standards (GixSQL / EXEC SQL) == === 6.1 SQL Formatting Rules === * All SQL keywords in UPPER CASE. * Each SQL clause on its own line, indented 11 spaces from column 8 (Area B +3). * Host variables prefixed with colon (<code>:</code>) and matching the <code>DB-</code> prefix. * '''Never use <code>SELECT *</code>''' β always name columns explicitly. * Every EXEC SQL block must be followed immediately by SQLCODE evaluation. * Named database connection (AT clause) required on all transactional SQL statements. Example β correctly formatted SELECT: <syntaxhighlight lang="cobol"> EXEC SQL AT primes SELECT ident, prime INTO :DB-IDENT, :DB-PRIME FROM primes WHERE ident = :DB-PRIME-IDENT END-EXEC EVALUATE SQLCODE WHEN 0 MOVE 0 TO DAL-RESULT WHEN 100 MOVE 2 TO DAL-RESULT WHEN OTHER MOVE 1 TO DAL-RESULT PERFORM E000-HANDLE-SQL-ERROR END-EVALUATE </syntaxhighlight> === 6.2 SQLCODE Handling === Treat SQLCODE as the sole success/failure signal after every SQL statement: {| class="wikitable" style="width:100%" ! SQLCODE !! Meaning !! Required Action |- | <code>0</code> || Success || Set <code>DAL-RESULT = 0</code>; continue. |- | <code>100</code> || No data / EOF || Set <code>DAL-RESULT = 2</code>; handle gracefully β not an error. |- | Negative || SQL error || Set <code>DAL-RESULT = 1</code>; log <code>SQLERRMC</code>; PERFORM E000-HANDLE-SQL-ERROR. |} Use numeric SQLCODE <code>100</code> (not <code>+100</code>) to detect end-of-cursor; this is portable across GixSQL and ANSI SQL. Do not use bare <code>IF SQLCODE = 0 β¦ ELSE</code> for cursor fetch β end-of-data is not an error. === 6.3 Cursor Standards === * Declare cursors in the DATA DIVISION (compile-time), not inline in the PROCEDURE DIVISION. * Always open a cursor inside a named transaction (<code>START TRANSACTION</code>). * Always use <code>FETCH β¦ INTO</code> with individually named host variables β never rely on implicit column ordering. * Close cursors explicitly before disconnecting; do not rely on connection reset to close implicitly. * One cursor per paragraph; do not interleave two cursor-fetch loops. === 6.4 Transaction Management === {| class="wikitable" style="width:100%" ! Event !! Required Action !! Location |- | After DB connect || <code>START TRANSACTION</code> if reading with a cursor || S000-series (cursor open) |- | Successful write (INSERT/UPDATE) || <code>COMMIT</code> after each logical unit of work || S000-series (write paragraph) |- | Error on write || <code>ROLLBACK</code>; set <code>DAL-RESULT = 1</code> || E000-HANDLE-SQL-ERROR |- | Before DB disconnect || <code>COMMIT</code> or <code>ROLLBACK</code> pending work || Z000-PROGRAM-END |} {{Ambox | type = notice | text = '''Defect pattern to avoid''' β The pgcobol reference implementation has the COMMIT statement commented out in the INSERT paragraph (<code>r83-write-prime</code>). This relies on PostgreSQL auto-commit and is not portable. Always include explicit COMMIT/ROLLBACK. }} === 6.5 Connection Management === * Use a named connection alias in every <code>EXEC SQL AT <alias></code> statement. * The alias must be consistent across all paragraphs in a program β mismatch causes a runtime alias-not-found error (pgcobol defect D-03). * Store connection parameters in WORKING-STORAGE named constants using the <code>DB-</code> prefix. * Move credentials to environment variables in production; hard-coded passwords in source are a security risk (pgcobol defect D-04). ---- ---- ''[[Cobol programming standards|β Back to index]]''
Summary:
Please note that all contributions to Webhuis wiki are considered to be released under the GNU Free Documentation License 1.3 or later (see
Project:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Template:Ambox
(
edit
)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Voorpagina
Cobol and PostgreSQL
PostgreSQL
CFEngine
Proxmox
Webhuis Kennisbank
Basale infra
Webhuis bouwstenen
Webhuis configuratie
Webhuis Infra
Webhuis Support
Webhuis Raspberry
Opzet Applicaties
Business Applicaties
Community portal
Current events
Recent changes
Random page
Help
sitesupport
Tools
What links here
Related changes
Special pages
Page information