Program Structure I
Program Structure I
Program Structure I
Program Structure I
EXEC SQL
COBOL
SQL statement END-EXEC.
EXEC SQL
FORTRAN X SQL statement
dept C01
SQL
. . . WHERE WORKDEPT = :dept
000190 JONES
empno name
© Copyright IBM Corporation 2005
Host Variables - Update - C Program
SQL statement
UPDATE TEMPL
SET SALARY = SALARY * 1.05
WHERE JOBCODE = 54
54 1.05
code percent
© Copyright IBM Corporation 2005
Host Variables - To Receive a Value
TO RECEIVE A VALUE
SELECT . . . INTO :a . . .
empid
GRAPHIC CHAR
FLOAT
Java
DB2
PL/I REFERENCE
LIBRARY
VARCHAR
DECIMAL COBOL
INTEGER
a value aind -
EXEC SQL
SELECT JOBCODE,WORKDEPT,PHONENO
INTO :JC :JCI, :DPT, :PHO:PHOI
FROM TEMPL
WHERE EMPNO = :ID;
EMPNO LASTNAME JOBCODE WORKDEPT PHONENO
(6) (20) 0-99 (3) (10)
000070 JOHNSON 54 C01 5137853210
000120 SCOTT ? C01 8592743091
000320 MILLIGAN ? C01 ?
id jc jci
dpt
pho phoi
© Copyright IBM Corporation 2005
Testing for NULL on Select - C Program
EXEC SQL
SELECT PHONENO, SEX
INTO :phoneno :phoneind, :sex
FROM TEMPL
WHERE EMPNO = :eno;
if (phoneind < 0)
null_phone ();
else
good_phone ();
if (some condition)
phoneind = -1;
else
phoneind = 0;
EXEC SQL
UPDATE TEMPL
SET PHONENO = :newphoneno :phoneind
WHERE EMPNO = :eno;
HOST INDICATOR
VARIABLE VARIABLE COLUMN
:CD :CDI JOBCODE
60 0 60
SELECT/FETCH
UNCHANGED <0 NULL
UPDATE/INSERT 50 0 50
N/A <0 NULL
S
Q Information / SQLCODE
Program L
C
A
Executable SQL
DB2
SQLCODE Detailed
Possibly platform dependent
Numeric
INTEGER CHAR(5)
CONDITION SQLCODE SQLSTATE REQUEST STATUS
(MORE)
NOTFOUND +100 "02nnn" DATA NOT FOUND
SUCCESS "00000"
0
SUCCESS
PROGRAM:
SQLCA NUMBER
SQLCODE OF ROWS
SQLWARN0 AFFECTED
SQLERRD(3) DB2
INSERT T
ES
U
E Q
UPDATE R
DELETE
Condition:
SQLERROR
Negative SQLCODE
SQLWARNING
Positive SQLCODE (not +100)
Or SQLWARN0 = 'W'
NOT FOUND
SQLCODE = +100
Action:
GO TO :X
Control transferred to statement labelled X
CONTINUE
Program continues with next statement
Used to cancel effect of prior WHENEVER
...
SQLCA;
};
EXEC SQL BEGIN struct sqlca sqlca;
DECLARE SECTION;
INCLUDE SQLCA
Establish connection
Do database processing
COMMIT