View previous topic :: View next topic
|
Author |
Message |
rakesh17684
New User
Joined: 08 Oct 2006 Posts: 61 Location: San Diego
|
|
|
|
Hi,
I am trying to use a host variable in a query for a COBOL DB2 program.
The variable is declared in the file section
Code: |
DATA DIVISION.
FILE SECTION.
FD INPT-FILE
RECORDING MODE IS F
BLOCK CONTAINS 0 RECORDS.
01 INPT-DATA.
05 INPT-FR-CODE PIC S9(04) USAGE COMP. |
--------------
when the variable is used as host variable for the queries inside the program . the query fails.
When the same variable is moved to a working section/DCLGEN variable it works fine.
What is the reason this happens .can we avoid this ?I am trying to reduce the number of variables declared.
Thanks in advance for any explanation or solution given. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
What does your manual say?
How do you feel this "reduces the number of variables" and why would you want to do that? |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
only thing that failed was the TS providing enough info......
what is the data definition of the variable in W-S (dclgen)
and as Bill mentioned, why are you so worried about number of variables in your program?
do you have some idiot at your site that says the number of variables must be keep to a minimum?
the biggest problem with using the FD, is that it must be open before it can be addressed.
and since all the addresses for the host variables are passed to db2 prior to your first instruction after PROCEDURE DIVISION,
i can understand why you are having problems........... |
|
Back to top |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
If I recall correctly : addresses within file section vary within a block |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Generally, yes. Read a record, new address. Write a record, new address.
So, no address before the first program instruction, and then a changing address.
I'd even suspect it tellls you in some manual somewhere not to even try this. |
|
Back to top |
|
|
don.leahy
Active Member
Joined: 06 Jul 2010 Posts: 765 Location: Whitby, ON, Canada
|
|
|
|
Yes, I am pretty sure it is documented, but in IBM-speak. Years ago, when I first read that section my reaction was "WTF?" After some research, I figured out what they meant and boiled it down to a simple rule of thumb: Always define DB2 Host Variables in Working Storage. Never use the FD area, never use Linkage. (Local Storage is okay too, but who uses that?).
My other rule of thumb: Where possible, always use the host variables defined in the DCLGEN. You can't go wrong that way. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
quoting from the manual
Quote: |
You must explicitly declare
all host variables and host
variable arrays that are used
in SQL statements in the
WORKING-STORAGE SECTION or
LINKAGE SECTION of your
program's DATA DIVISION. |
the must applies also to the where |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
for linkage section, you need to add a line of code to your program to move '1'
(or space, i forget which,
but you can tell how to turn the switch back on for each call
by looking at the compiler listing)
to INIT-FLAG as the first instruction in your program.
that way, each time the program is CALLed,
db2, again, goes thru the code which passes the addresses to db2.
because one needs to know what is going on in the computer,
it is generally safer to tell everyone only to use working/local storage.
linkage requires resetting of the INIT-FLAG, |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
And presumably don't get clever with SET ADDRESS, just because it is allowed in the Linkage Section. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
a little further clarification.
you have a db2 module that is CALLed by everybody to select some rows.
first time it is CALLed by prg-1 USING PGM-1 WORKING-STORAGE.
db2 module provides the addresses linkage section variables to db2, which are prg-1 working-storage.
then prg-2 CALLs db2 module with prg-2 WORKING-STORAGE.
if the db2 module does not have the INIT-FLAG reset logic,
which would cause db2 to be CALLed for addressing,
all the sql would use pgm-1 data areas for host-variables.
even though pgm-2 CALLed the db2 module (with pgm-2 using pgm-2 working-storage)
sad but true. |
|
Back to top |
|
|
don.leahy
Active Member
Joined: 06 Jul 2010 Posts: 765 Location: Whitby, ON, Canada
|
|
|
|
In an IMS online environment, it can get even worse because the subroutine may remain in storage between transactions. Even if only one program calls the subroutine, that calling program may not be loaded in the same area of storage from one transaction to the next. It *usually* works okay, but there are no guarantees. I have had some very interesting debugging sessions caused by this issue.
Rather than try to explain all this to, er, less technically minded developers, I usually just trot out the rule of thumb. The smarter ones will ask why; there's no point in trying to explain it to the others. |
|
Back to top |
|
|
|