Bill wrote:
(...)
Hi All,
I've always assumed the DB2 Host variables must be in 'working storage'
If you try using host variables contained within a FD record
description - you never get the intended result - so I presume DB2 isn't
aware of any record reads causing the address of the fields within the FD to
change. I presume passing addresses via linkage has the same limitations. So
I alway move the items from linkage to WS and use them from there.
(...)
The problem with using host variables defined in LINKAGE SECTION is
that it usually works ;)
Error appears when you call your program containing sql queries from
two different programs.
Example:
PROG1 calls PROGX and PROGY.
PROGX and PROGY call PROGSQL which uses LINKAGE SECTION variables as
host variables.
Then "strange" things could happen. I was getting SQLCODE +100 on the
first query in the program. And I am sure that the record I was
reffering to was present in the table. After some experimenting it
turned out that the DB2 refers to a different place in memory than
COBOL variables. It looks like during the first call of PROGSQL DB2
remembers the addresses of host variables. During the second call the
addresses are not "reinitialized" and DB2 refers to the wrong place in
memory.
If the variables from WORKING STORAGE are used as host variables
everything is OK.
My guess is that because WORKING STORAGE is allocated separately for
each of the
calls of the PROGSQL program - DB2 reinitializes the addresses every
time the program is called. When the variables from LINKAGE SECTION are
used as host variables DB2 decides that it is not necessery, to
initialize the adresses again.
If the PROGSQL would be called only from PROGX using LINKAGE SECTION
variables as host variables it would propably cause no harm (although
I'm not completly sure of it).
Anyway - to solve the problem I decided to copy the contents of LINKAGE
SECTION variables to some WORKING STORAGE variables and use them in
queries. It helped.
I learned that I could also set the SQL-INIT-FLAG at the beginning of
the program to tell
DB2 to reinitialize the addresses - but I didn't try it.
I was wondering if someone have similar problems and maybe know some
more about how DB2 decides to initialize or not to initialize the host
variables addresses.
regards
Michal Osada