Was this helpful?
The Scope of Variables
You can reference all variables declared in an Embedded SQL declaration section and the preprocessor accepts them from the point of declaration to the end of the file. This may not be true for the Fortran compiler, which allows references to variables only in the scope of the program unit in which they were declared. If you have two unrelated subprograms in the same file, each of which contains a variable with the same name to be used by Embedded SQL, do not redeclare the variable to Embedded SQL. The preprocessor uses the data type information supplied the first time you declared the variable.
In the following program fragment, the variable dbname is passed as a parameter between two subroutines. In the first subroutine, the variable is a local variable. In the second subroutine, the variable is a formal parameter passed as a string to be used with the connect statement. The declaration of dbname in the second subroutine must not occur in an Embedded SQL declaration section. In both subroutines, the preprocessor uses the data attributes from the variable's declaration in the first subroutine.
Example: Variable declaration
            subroutine Scopes
            exec sql include sqlca
            exec sql begin declare section 
                        character*20 dbname 
            exec sql end declare section

C Prompt for and read database name 
            type *, 'Database: ' 
            accept *, dbname 
            call OpenDb(dbname) 
                        ...

            end
            subroutine OpenDb(dbname)

            exec sql include sqlca
            character*(*) dbname
            exec sql whenever sqlerror stop 
C Declared to SQL in first subroutine 
            exec sql connect :dbname 
               ...

    end
Take special care when using variables in a DECLARE CURSOR statement. The variables used in such a statement must also be valid in the scope of the OPEN statement for that same cursor. The preprocessor actually generates the code for the declare at the point that the OPEN is issued and, at that time, evaluates any associated variables.
For example, in the following program fragment, even though the variable "number" is valid to the preprocessor at the point of both the DECLARE CURSOR and OPEN statements, it is not an explicitly declared variable name for the Fortran compiler at the point that the OPEN is issued, possibly resulting in a runtime error. Because Fortran allows implicit variable declarations (although Embedded SQL does not), the compiler itself does not generate an error message.
C This example contains an error
               subroutine IniCsr
              exec sql include sqlca
              exec sql begin declare section
C A local variable
                        integer number
               exec sql end declare section
              exec sql declare cursor1 cursor for
            1     select ename, age
            2     from employee
            3     where eno = :number
C Initialize "number" to a particular value
                      ...

              end
              subroutine PrcCsr
              exec sql include sqlca
              exec sql begin declare section
                      character*16 ename
                      integer      eage
               exec sql end declare section
C Illegal evaluation of "number"
              exec sql open cursor1
              exec sql fetch cursor1 into :ename, :eage
                    ...

              end
You must issue the INCLUDE SQLCA statement in each subprogram that contains Embedded SQL statements.
Last modified date: 11/28/2023