Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VARCHAR - good explanation/example

Status
Not open for further replies.
M

Member 310024

Guest
I am having trouble finding a good explanation/example
of how to use VARCHAR in a COBOL/DB2 program.
I am aware of the 2 x 49 levels concept,
the 1st 49 level being a 2-byte length,
and the 2nd 49 being a pic x(?) that is big enough
to hold the 'max-size' variable.
eg
03 DESCRIPTION.
49 DESCRIPTION-LENGTH PIC S9(04) COMP-3.
49 DESCRIPTION-TEXT PIC X(100).

In my embedded SQL SELECT, do I SELECT INTO DESCRIPTION
or DESCRIPTION-TEXT? I presume DESCRIPTION!

But say I want to do an INSERT or an UPDATE.
How do I set up my fields?
Do I say MOVE 'My Description' to DESCRIPTION
and then issue my UPDATE statement,
or do I say:
MOVE 'My Description' to DESCRIPTION-TEXT
and then MOVE 14 TO DESCRIPTION-LENGTH.
(Note: 'My Description' is 14 characters long.)
and then issue my UPDATE statement.

If it's the latter, then it means that my UPDATE
statement will have to break the pattern of the
target-name not matching the source-name
eg
EXEC SQL
UPDATE TABLE-XYZ
SET
DESCRIPTION = :DESCRIPTION-TEXT
WHERE
etc ....
END-EXEC.

ie the source name is DESCRIPTION-TEXT, but
the target name is DESCRIPTION.

Is this how it works?






 
Terminate,

work with your level 3, in this case description, within your SQL.

If you select description, description-text and description-length will be set up for you. You can then work in COBOL with description-text.

If your updating, move the data into description-text, work out it's length and put that in description-length. Then once again refer to it as description in you SQL.

Cheers
Greg
 
Most languages these days use null-terminated strings for handling VARCHARs. But because COBOL needs to know both the maximum size and the actual size of the VARCHAR, the variables have to be split into text and length parts. In your COBOL code you have to mess around with the two parts (and make sure you don't accidentally set the VARCHAR-LENGTH > LENGTH(VARCHAR-TEXT), but in your SQL call you should use the 03 level name as the host variable, as Greg describes above.

(Hi Greg - how's it going?)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top