The precompiler supports nested COBOL programs and COBOL’s rules for variable scoping. Host variables can use the is global and is external clauses. Following is a nested example:
IDENTIFICATION DIVISION.
PROGRAM-ID. outer.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. xyz.
OBJECT-COMPUTER. xyz.
DATA DIVISION.
WORKING-STORAGE SECTION.
exec sql begin declare section end-exec.
01 global-var is global pic x(10).
01 not-global-var pic x(10).
01 shared-var is external pic x(10).
exec sql end declare section end-exec.
procedure division.
p0.
. . .
IDENTIFICATION DIVISION.
PROGRAM-ID. inner.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. xyz.
OBJECT-COMPUTER. xyz.
DATA DIVISION.
WORKING-STORAGE SECTION.
procedure division.
p0.
. . .
* This is legal because global-var was
* declared using is global
exec sql
select au_lname into :global-var
where au_id = "998-72-3567"
end-exec.
* This is not legal because not-global-var was
* not declared using is global
exec sql
select au_lname into :not-global-var
where au_id = "998-72-3567"
end-exec.
. . .
end program inner.
end program outer.
IDENTIFICATION DIVISION.
PROGRAM-ID. nonest.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. xyz.
OBJECT-COMPUTER. xyz.
DATA DIVISION.
WORKING-STORAGE SECTION.
exec sql begin declare section end-exec.
01 local-var pic x(10).
01 shared-var is external pic x(10).
exec sql end declare section end-exec.
procedure division.
p0.
. . .
* This is legal.
exec sql
select au_lname into :local-var
where au_id = "998-72-3567"
end-exec.
* So is this.
exec sql
select au_lname into :shared-var
where au_id = "998-72-3567"
end-exec.
. . .
end program nonest.