DB2 Application Programming
DB2 Application Programming
Overview
Program Structure
Overview
Question
Question
Host Languages
COBOL
PL/1
C Assembler FORTRAN
Use host variables to provide values on the WHERE clause or receive values from SELECT statements
Use appropriate techniques to process null data
Overview
Static SQL
Dynamic SQL
Embedded SQL
Delimited all SQL statements
SQL Delimiters
Overview
Host Variables
OPTIONAL, used to allow setting a value before statement is executed Host language label in SQL statement preceded by a colon Host variable must match column data type
EXEC SQL
All COBOL host variables must be explicitly declared in the DATA DIVISION.
A colon ( : ) must precede all host variables in an SQL statement The names of the host variables must be unique within the program.
DATE TIME
PICX(10) PICX(08)
TIMESTAMP PIC X (26) String representation of timestamp has the format: yyy-mm-dd-hh.mm.ss.nnnnnn
EXEC SQL SELECT EMPNO, LAST, SEX INTO : WS-EMP-STRUCT FROM EMPLOYEE WHERE EMPNO = :WS-EMP
05 WS-EMPNO PIC S9(9) USAGE COMP. 05 WS-LAST PIC X (30). 05 WS-SEX PIC X(01).
END-EXEC.
How to include Host variable Declaration and SQL Communication Area predefined Declaration in Program
Overview
INCLUDE
EXEC SQL
Member-name
Member of the partitioned data set . The member can contain any COBOL variable declarations, source statements and any SQL statements other than an INCLUDE statement.
Overview
SQLCA
SQLSTATE is set by the database manager after execution of each SQL statement. Application programs can check the success of the execution of SQL statements by testing SQLSTATE instead of SQLCODE.
Question
SQLCA Warnings
Example Program
Overview
Overview
DCLGEN-Input
DCLGEN- Output
DCLGEN- Output
DCLGEN - Output
DB2 PRE-COMPILATION
BIND JCL
BIND JCL
Program Preparation
PRECOMPILER
COMPILER BINDER PLAN and PACKAGE
PRECOMPILE COMPILE
LINK-EDIT
BIND & RUN
What does DB2 Pre-compile does when you submit a Embedded SQL Program as Source ?.
Question
PRE-COMPILE Process
Embedded SQL Program has Application Code SQL Statements Before the program can be
compiled by the appropriate source code language compiler.
PRECOMPILER prepares the source program for compilation by replacing EXEC SQL by a CALL and commenting out the EXEC SQL statement.
What is the Purpose of Database Request Module (DBRM) in DB2 Program Preparation ?.
Question
Overview
After DB2 PRE-COMPILATION the modified Cobol program with CALL statements ( instead of EXEC SQL) is now ready to be compiled and link-edited. LINK-EDIT process will have to include the necessary modules for the call to work properly. Modified Embedded Cobol source file is
After DB2 PRECOMPILATION the modified Cobol program with CALL statements ( instead of EXEC SQL) is now ready to be compiled and link-edited. LINK-EDIT process will have to include the necessary modules for the call to work properly. Modified Embedded Cobol source file is
Produce an
Overview
Bind Process
When the bind file or DBRM is bound to the database, a package is created in the database. package contains instructions about how to execute the SQL statements that were included in the source code.
Bind Process
Run
Run
Run Process
Accepts Input
Executes Load Module Retrieves Information from DB2 Package when program call SQL statements
Overview
PRE-COMPILE
BIND JCL
OUTPUT
PRE-COMPILE
BIND JCL
OUTPUT
Overview
PRE-COMPILE
BIND JCL
BINDJCL
BINDJCL
OUTPUT
OUTPUT
Special Registers
EXEC SQL SET :WS-TS = CURRENT TIMESTAMP END-EXEC. EXEC SQL SET : WS-USER = USER END-EXEC.
Overview
Indicator Variables
Indicator variables are small integers that are used to :
Indicate whether the values of the associated host variables are NULL Verify that the value of a retrieved character string has not been truncated
Insert and update NULL values from host variables into columns.
INDICATOR VARIABLES
A variable of SMALLINT data type associated with a host variable Must be declared. Is preceded by a colon and directly follows its associated host variables, when used in an SQL statement. EXEC SQL SELECT INTO FROM WHERE END EXEC.
Note *
IV HIREDATE is the indicator variable associated with the host variable HV - HIREDATE
INDICATOR VARIABLES
-1
is NULL
>0 is a truncated character string; the value in the indicator variable is the length of the character string before truncation.
TO WS-LASTNAME-IND.
SELECT LASTNAME
MOVE
MOVE
2120
SPACES
TO WS-EMPNO.
TO WS-PHONE-NO. TO WS-PHONE-NO-IND
MOVE - 1
EXEC SQL UPDATE EMPLOYEE SET PHONE = :WS-PHONE-NO :WS-PHONE-NO-IND WHERE EMPNO = :WS-EMPNO END-EXEC.
EXEC SQL INSERT INTO EMPLOYEE (EMPNO, SEX, AGE,PHONE) VALUES ( :WS-EMPNO , :WS-SEX, :WS-AGE, :WS-PHONE-NO :WS-PHONE-NO-IND) END-EXEC.
Indicator Variables
MOVE ZEROES
..... UPDATE-PARA.
TO WS-PHONE-NO-IND.
PERFORM UPDATE-PARA.
EXEC SQL
UPDATE EMPLOYEE SET PHONE = :WS-PHONE-NO :WS-PHONE-NO-IND WHERE EMPNO = :WS-EMPNO
END-EXEC.
PRE-COMPILE
BIND JCL
output
OUTPUT
SQL CODE
EXEC SQL SELECT EMPNO, LASTNAME, SEX INTO :WS-EMPNO, :WS-LAST, :WS-SEX
FROM EMPLOYEE WHERE EMPNO= :WS-EMPNO END-EXEC. EVALUATE SQLCODE WHEN 0 DISPLAY LAST NAME IS WS-LAST WHEN 100 DISPLAY WS-EMPNO DOES NOT EXIST END-EVALUATE
RESULT TABLE
DECLARE CURSOR
EXEC SQL DECLARE CUREMP01 CURSOR FOR SELECT EMPNO, SEX, AGE, NAME FROM EMPLOYEE WHERE JOIN_DATE > : WS-JOIN-DATE END-EXEC.
OPEN
FETCH
Syntax : EXEC SQL FETCH CURSOR-NAME INTO :host-variable1, :host-variable2, .. END-EXEC.
Example : EXEC SQL FETCH CUREMP01 INTO :WS-EMPNO, :WS-SEX, :WS-AGE END-EXEC.
WS-EMPNO WS-JOIN-DATE
SQLCODE
FETCH
0 0 0 100
Handling End-of-data
MAIN-PARA.
PERFORM FETCH-PARA
FETCH-PARA
....
CLOSE
CLOSE cursor-name
END-EXEC. Example : EXEC SQL
CLOSE CUREMP01
END-EXEC.
PRE-COMPILE
BIND
OUTPUT
Output
Output
OUTPUT
FOR UPDATE OF
EXEC SQL
PERFORM OPEN-PARA
PERFORM FETCH-PARA. PERFORM UNTIL SQLCODE =100 EXEC SQL DELETE FROM EMPLOYEE WHERE CURRENT OF CUREMP01 END-EXEC PERFORM FETCH-PARA
END-PERFORM.
EXEC SQL DECLARE cursor-name CURSOR WITH HOLD FOR SELECT column-name-list FROM table-name WHERE search-condition FOR UPDATE OF column-name END-EXEC.
Dynamic SQL
Statement: Acquired during execution,and function can vary and can be done to different tables and columns
Dynamic : Object or Action not known Bind: Static Dynamic : is bound once : is bound every time.
EXECUTE IMMEDIATE
Prepare and Execute the SQL statement present in the COBOL host variable WS-STMT MOVE DELETE FROM EMPLOYEE TO WS-STMT-TEXT MOVE 50 EXEC SQL EXECUTE IMMEDIATE :WS-STMT END-EXEC. TO WS-STMT-LEN
Parameters Markers
DB2 LOCKS
Overview
DB2 LOCKS
DB2 LOCKS * * *
Are managed by IMS Resource Lock Manager, IRLM Are used to control Concurrency and guarantee the integrity of each users data Prevent access to uncommitted data
CONCURRENCY CONTROL
Concurrency is a situation wherein DB2 allows more than one application program to access same data at the same time.
Concurrency needs to be controlled due to many undesirable effects such as i LOST UPDATES (or Double Updating),
ii
iii
Both read the same Row from the table Both supply new values for one of its columns based on what they read.
PROBLEM:
If X updates the row with its value and then Y updates the same row, Xs updates are lost
If Xs value is not later committed but backed out, then Ys calculations are based on uncommitted and presumably invalid data
An application program X reads a row form the table and goes on to process other SQL statements. X again reads the row it read the first time and must find the same values in it.
PROBLEM
Without Concurrency control, application program Y could have changed the same row between two reads of program X.
Overview
Determines how large a portion of the resource is being locked The LOCKSIZE parameter of CREATE TABLESPACE statement defines the locking protocol DB2 being locking levels are:
Tablespace locking
Page locking
Determines the length of time the lock is held Varies according to when the lock is acquired and when it is released
The duration of any tablespace lock held by an application program can be controlled by means of ACQUIRE and RELEASE parameters of BIND ACQUIRE specifies when resources should be allocated, I.e., when the locks should be acquired
RELEASE specifies when resources should be deallocated, I.e., when the locks should e freed or released. Both the above parameters have two sub-parameters:
For ACQUIRE
USE ALLOCATE
For RELEASE
COMMIT DEALLOCATE
Influence the initial lock chosen by DB2 especially its duration Ensure data consistency, an application program can be bound with any of the two values Page lock is held only while the cursor is positioned on at page. When the cursor moves to another page, the lock is leased. When a page is locked concurrent application programs cannot update or delete a row of locked page If an application program updates or deletes data, the lock is held until the data is committed Applies only to data is read. All changed data remains locked until COMMIT or ROLLBACK
Cursor Stability
U(UPDATE)
X(EXCLUSIVE)
TABLE EXCLUSIVE IS(INDENT SHARE)
IX(INDENT EXCLUSIVE)
PAGE EXCLUSIVE : Concurrent application programs can only read and update data of other pages.