Hi gouravar,
after a long weekend, I found some time to answer your question
When program 'A' gets compiled, a DBRM 'A' is being generated. It includes all the SQL - and additional information like host variables etc - exctracted from your program 'A' source code. At this time, the DB2 precompiler has no knowledge of the existence of a program 'B'. When program 'B' gets compiled, a DBRM 'B' is being generated - here also, DB2 has no knowledge of a program 'A', it only sees program 'B'.
If you look at the DB2 precompiler output listing, you may have a look at those places where the precompiler replaced your
EXEC SQL ... END-EXEC statements. You'll find new structures (SQL-PLISTn) and calls to routines (PERFORM SQL-INITIAL, ...) but none of those places have a reference to your cursor name. So if the cursor name is not known to DB2 either at precompile nor at runtime, DB2 does not care about cursor names...
I've created A and B samples. Both use cursor name 'C1' and access different tables:
IDENTIFICATION DIVISION. | IDENTIFICATION DIVISION.
PROGRAM-ID. A. <-> PROGRAM-ID. B.
ENVIRONMENT DIVISION. | ENVIRONMENT DIVISION.
CONFIGURATION SECTION. | CONFIGURATION SECTION.
DATA DIVISION. | DATA DIVISION.
WORKING-STORAGE SECTION. | WORKING-STORAGE SECTION.
01 TEMP PIC X(1). | 01 TEMP PIC X(1).
01 DISCOMP PIC +9(9). | 01 DISCOMP PIC +9(9).
EXEC SQL INCLUDE SQLCA END-EXEC. | EXEC SQL INCLUDE SQLCA END-EXEC.
EXEC SQL DECLARE C1 CURSOR FOR SELECT <-> EXEC SQL DECLARE C1 CURSOR FOR SELECT
IBMREQD FROM SYSIBM.SYSDUMMY1 END-EXEC. <-> 'X' FROM SYSIBM.SYSDUMMYE END-EXEC.
PROCEDURE DIVISION. | PROCEDURE DIVISION.
MAIN SECTION. | MAIN SECTION.
EXEC SQL OPEN C1 END-EXEC. | EXEC SQL OPEN C1 END-EXEC.
MOVE SQLCODE TO DISCOMP | MOVE SQLCODE TO DISCOMP
DISPLAY 'A OPEN <' DISCOMP '/' SQLERRMC '>' | DISPLAY 'B OPEN <' DISCOMP '/' SQLERRMC '>'
CALL 'B' <|
EXEC SQL FETCH C1 INTO :TEMP END-EXEC. | EXEC SQL FETCH C1 INTO :TEMP END-EXEC.
MOVE SQLCODE TO DISCOMP | MOVE SQLCODE TO DISCOMP
DISPLAY 'A FETCH <' DISCOMP '/' SQLERRMC | DISPLAY 'B FETCH <' DISCOMP '/' SQLERRMC
'/' TEMP '>' | '/' TEMP '>'
EXEC SQL CLOSE C1 END-EXEC. | EXEC SQL CLOSE C1 END-EXEC.
MOVE SQLCODE TO DISCOMP | MOVE SQLCODE TO DISCOMP
DISPLAY 'A CLOSE <' DISCOMP '/' SQLERRMC '>' | DISPLAY 'B CLOSE <' DISCOMP '/' SQLERRMC '>'
GOBACK | GOBACK
. | .
MAIN-EXIT. EXIT. | MAIN-EXIT. EXIT.
Running programs results in:
A OPEN <+000000000/ >
B OPEN <+000000000/ >
B FETCH <+000000000/ /X>
B CLOSE <+000000000/ >
A FETCH <+000000000/ /Y>
A CLOSE <+000000000/ >
I'm now back enjoying the next weekend