INF: Data Conversion Sample Program in Embedded SQL for COBOL

ID Number: Q66752

1.10 1.11 4.20

OS/2

Summary:

The code listed below demonstrates several data conversion methods for

bringing down SQL Server data and displaying it in a Microsoft

Embedded SQL for COBOL program.

This example selects a varchar(80) field, a money field, an int field,

and a datetime file into COBOL program variables and displays them.

The title field is straightforward. Price is brought down using a "v"

in the picture clause representing the assumed decimal place, and is

moved to a variable with a decimal point (.) to actually print the

decimal point. The reason for this is that the decimal point is not

accepted in the picture clause of a host variable.

Ytd-sales is also straightforward, except that it is moved into

another program variable so that the plus (+) sign and preceding 0

(zero) would not be displayed. The pubdate field is converted on the

SQL Server end and just returns the month, day, and year.

Sample Code

-----------

WORKING-STORAGE SECTION.

EXEC SQL INCLUDE SQLCA END-EXEC

EXEC SQL BEGIN DECLARE SECTION END-EXEC

01 title pic x(80).

01 price pic s9(9)v9(9) comp-3.

01 ytd-sales pic s9(4) comp-3.

01 pubdate pic x(11).

EXEC SQL END DECLARE SECTION END-EXEC

01 price-convert pic z(3).9(2).

01 ytd-sales-convert pic z(4).

PROCEDURE DIVISION.

EXEC SQL

select title, price, ytd_sales, convert(char(11),pubdate)

into :title, :price, :ytd-sales, :pubdate

from titles

where title_id = "BU1111"

END-EXEC

* Ignore any warnings, sqlcode = 1. *

if sqlcode = 0 or sqlcode = 1

move price to price-convert

move ytd-sales to ytd-sales-convert

display title

display "Has sold " ytd-sales-convert " copies this year, at

$"price-convert " a copy."

display "It was published on " pubdate

else

perform sql-error

end-if

stop run.

sql-error.

display "SQL error SQLCODE=" sqlcode.

Additional reference words: Embedded SQL for COBOL