Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3890

"SQLCODE" or "SQLSTATE" should be tested after each SQL statement

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Message:
      Test "SQLCODE" or "SQLSTATE" before proceeding.
    • Highlighting:
      Hide

      First statement after end-exec

      Show
      First statement after end-exec
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Targeted languages:
      PL/I, PL/SQL, RPG
    • Covered Languages:
      Cobol
    • Irrelevant for Languages:
      ABAP, C#, C, C++, CSS, Flex, HTML, Java, JavaScript, Objective-C, PHP, Python, Swift, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      After the execution of each SQL statement (other than DECLARE CURSOR, DECLARE TABLE and DECLARE VARIABLE), the value of SQLCODE or SQLSTATE should be checked before proceeding. A 0 SQLCODE value means the statement succeeded, a positive value means success with a warning, and a negative value indicates an error. Proceeding without checking could put your program or your data in a bad state.

      Noncompliant Code Example

      EXEC SQL
        SELECT name INTO :username FROM user WHERE id = :userid
      END-EXEC.
      
      DISPLAY username.    *> Noncompliant
      

      Compliant Solution

      EXEC SQL
        SELECT name INTO :username FROM user WHERE id = :userid
      END-EXEC.
      
      IF SQLCODE = 0 THEN
        DISPLAY username
      END-IF.
      

      Exceptions

      When the value of SQLCODE or SQLSTATE is not checked but transferred to another variable for later use, no issue is raised.

      EXEC SQL
        SELECT name INTO :username FROM user WHERE id = :userid
      END-EXEC.
      MOVE SQLCODE TO SQL-RETURN-CODE
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ann.campbell.2 Ann Campbell
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: