Professional Documents
Culture Documents
Session Plan
• SPUFI Hands On
• DCLGEN Hands On
• Cursors
• Embedded SQL syntax is almost same as the SQL syntax used in interactive
mode.
• These variables are defined in the host language and are referred to as the
host variables.
• Static SQL: The application programmer knows in advance the SQL statement
completely.
• They are referred as host variables because they are used for receiving data
from the table or inserting data to the table.
• One must declare host variables for all values that are to be passed between
the application program and DB2.
• The data types of the DB2 columns and corresponding host variables must be
compatible.
• The host variables can not be group items, the only exception to this rule is the
variable corresponding to VARCHAR.
EXEC SQL
INCLUDE STUD627
END-EXEC
WORKING-STORAGE SECTION.
EXEC SQL
INCLUDE SQLCA
END-EXEC.
• All SQL statements other than INCLUDE and DECLARE TABLE must
appear in PROCEDURE DIVISION.
SELECT
EXEC SQL
SELECT EMPNO, SALARY INTO
:WS-EMPNO, :WS-SALARY FROM EMMPLOYEE
WHERE NAME = :WS-NAME
END-EXEC
Error Handling
IF SQLCODE = 0 CONTINUE
ELSE IF SQLCODE = -811 DISPLAY “MULTIPLE ROWS”
ELSE PERFORM C9000-ERROR-PARA.
EXEC SQL
INSERT INTO EMPLOYEE (EMPNO, NAME, SALARY)
VALUES (:WS-EMPNO, :WS-NAME, :WS-SALARY)
END-EXEC
Error Handling
IF SQLCODE = 0 CONTINUE
ELSE DISPLAY “UNIDENTIFIED ERROR OCCURRED”.
EXEC SQL
UPDATE EMPLOYEE SET SALARY = :WS-SALARY
WHERE EMPNO = 10878
END-EXEC
Error Handling
IF SQLCODE = 0 CONTINUE
ELSE DISPLAY “UNIDENTIFIED ERROR OCCURRED”.
DELETE
EXEC SQL
DELETE FROM EMPLOYEE WHERE EMPNO = :WS-EMPNO
END-EXEC
Error Handling
IF SQLCODE = 0 CONTINUE
ELSE DISPLAY “UNIDENTIFIED ERROR OCCURRED”.
MOVE -1 TO WS-SALARY-IND
EXEC SQL
INSERT INTO EMPLOYEE(SALARY)
VALUES:WS-SALARY:WS-SALARY-IND
END-EXEC
Error Handling
IF SQLCODE = 0 CONTINUE
ELSE DISPLAY “UNIDENTIFIED ERROR OCCURRED”.
• Conceptually they is a results table used by DB2 to contain the multiple results
of a query.
• They are data structures which hold some/all the results of a query.
2. DECLARE
4. OPEN
6. FETCH
8. CLOSE
Opening a Cursor
EXEC SQL
OPEN EMPCUR
END-EXEC
Closing a Cursor
EXEC SQL
CLOSE EMPCUR
END-EXEC
EXEC SQL
FETCH EMPCUR
INTO :WS-EMPNO,:WS-NAME,:WS-SALARY
END-EXEC
• Remarks:
Fetching to Update
EXEC SQL
DECLARE EMPCUR CURSOR
SELECT EMPNO, NAME, SALARY
FROM EMPLOYEE WHERE EMPNO > :WS-EMPNO
FOR UPDATE OF SALARY
END-EXEC
EXEC SQL
UPDATE EMPLOYEE
SET SALARY = :WS-SALARY
WHERE CURRENT OF EMPCUR
END-EXEC
• SPUFI Hands On
• DCLGEN Hands On
• Cursors