You are on page 1of 58

DB2

Session Plan
• • • • • • • SPUFI Hands On Introduction to Embedded SQL DCLGEN Hands On SQLCA Copy Book and its Use Single Row Manipulation Multiple Row Manipulation Cursors

Copyright © 2005, Infosys Technologies Ltd

2

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

3

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

4

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

5

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

6

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

7

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

8

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

9

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

10

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

11

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

12

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

13

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

14

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

15

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

16

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

17

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

18

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

19

ER/CORP/CRS/DB01/003 Version No:2.0a

Embedded SQL
• Embedded SQL refers to the use of standard SQL statements within a high-level language program such as COBOL, PL1, C, PASCAL, etc,. • • The high level language is called Host language Embedded SQL statements are preprocessed by SQL Pre processor before the application program is compiled

Copyright © 2005, Infosys Technologies Ltd

20

ER/CORP/CRS/DB01/003 Version No:2.0a

Embedded SQL
• Embedded SQL syntax is almost same as the SQL syntax used in interactive mode.

The output of a query is directed to a pre-defined set of variables instead of the terminal.

These variables are defined in the host language and are referred to as the host variables.

An additional INTO clause is placed after the SQL SELECT statement.

Copyright © 2005, Infosys Technologies Ltd

21

ER/CORP/CRS/DB01/003 Version No:2.0a

Types of SQL
• Static SQL: The application programmer knows in advance the SQL statement completely. 2. Dynamic SQL: The application programmer is unaware of the SQL statement in advance. This requires dynamic compilation and binding.

Copyright © 2005, Infosys Technologies Ltd

22

ER/CORP/CRS/DB01/003 Version No:2.0a

Host Variables(1 of 2)
• Host variables are just like any other variable of the high level language

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 host variables may appear anywhere in the WORKING-STORAGE SECTION.

The data types of the DB2 columns and corresponding host variables must be compatible.
Copyright © 2005, Infosys Technologies Ltd 23 ER/CORP/CRS/DB01/003 Version No:2.0a

Host variables (2 of 2)
• The host variables can not be group items, the only exception to this rule is the variable corresponding to VARCHAR.

The host variables may be grouped together in a host structure.

The host variables can be Redefined

The host variable cannot be Renamed.

Copyright © 2005, Infosys Technologies Ltd

24

ER/CORP/CRS/DB01/003 Version No:2.0a

Declaring Host Variables
• Need to declare in the working-storage section of your program

EXEC SQL BEGIN DECLARE SECTION END-EXEC. 01 EMPLOYEE-REC. 03 EMP-NO 03 EMP-NAME 03 EMP-ADDRESS. 49 EMP-ADDRESS-LEN 49 EMP-ADDRESS-TEXT EXEC SQL END DECLARE SECTION END-EXEC. PIC S9(4) COMP. PIC X(25). PIC S9(4) COMP. PIC X(15).

Copyright © 2005, Infosys Technologies Ltd

25

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

26

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

27

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

28

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

29

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

30

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

31

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

32

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

33

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

34

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

35

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

36

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

37

ER/CORP/CRS/DB01/003 Version No:2.0a

Using DCLGEN copybook

EXEC SQL INCLUDE STUD627 END-EXEC

Copyright © 2005, Infosys Technologies Ltd

38

ER/CORP/CRS/DB01/003 Version No:2.0a

SQLCA
• This is the communication area (a copybook containing some variables) through which DB2 passes the feedback of SQL execution to the program

In the working-storage section use

WORKING-STORAGE SECTION. EXEC SQL INCLUDE SQLCA END-EXEC.

Copyright © 2005, Infosys Technologies Ltd

39

ER/CORP/CRS/DB01/003 Version No:2.0a

SQLCA Copy Book (1 Of 3)
01 SQLCA. 03 SQLCAID 03 SQLCABC 03 SQLCODE 03 SQLERRM. 49 SQLERRML 49 SQLERRMC 03 SQLERRP 03 SQLERRD OCCURS 6 03 SQLWARN. 05 SQLWARN0 05 SQLWARN1 05 SQLWARN2 05 SQLWARN3 05 SQLWARN4 05 SQLWARN5 05 SQLWARN6 05 SQLWARN7 03 SQLSTATE PIC X(8). PIC S9(9) COMP. PIC S9(9) COMP VALUE 0. PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC S9(4) COMP. X(70). X(8). S9(9) COMP. X. X. X. X. X. X. X. X. X(5)
40 ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

SQLCA Copy Book (2 of 3)
03 SQLCAID 03 SQLCABC 03 SQLCODE For Reading dumps. Length of SQLCA (136 bytes). = 0 (Successful) = +ve (Exceptional condition) = -ve (Failure) Error Message Length. Error Message Text. Info about internal Error. Internal Error Code. Internal Error Code. Number of Rows affected by INSERT, UPDATE and DELETE. Estimate of resources needed by dynamic SQL statement. Info about dynamic SQL. Internal Error Code.
Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

03 SQLERRM. 49 SQLERRML 49 SQLERRMC 03 SQLERRP 03 SQLERRD(1) 03 SQLERRD(2) 03 SQLERRD(3) 03 SQLERRD(4) 03 SQLERRD(5) 03 SQLERRD(6)

41

SQLCA Copy Book (3 of 3)
05 SQLWARN0 05 SQLWARN1 05 SQLWARN2 05 SQLWARN3 05 SQLWARN4 05 SQLWARN5 05 SQLWARN6 05 SQLWARN7 ‘W’ if any other SQLWARNx fields are set to ‘W’. ‘W’ indicates truncation of character strings. ‘W’ indicates null values were ignored. Ex: AVG, COUNT. ‘W’ indicates more columns than host variables. ‘W’ indicates no WHERE clause for UPDATE or DELETE. ‘W’ indicates SQL/DS statement. ‘W’ for adjustment of DATE. Reserved for Future use.

Copyright © 2005, Infosys Technologies Ltd

42

ER/CORP/CRS/DB01/003 Version No:2.0a

Programming guidelines (1 of 2)
• • • Every SQL statement must be coded between columns 12 and 72. Every SQL statement must be delimited between EXEC SQL and END-EXEC. All the tables that are used in a program are to be declared in the WORKINGSTORAGE SECTION. This can be done using INCLUDE statement

All SQL statements other than INCLUDE and DECLARE TABLE must appear in PROCEDURE DIVISION.

Anything on a line within an SQL statement following two hyphens is treated as a comment. Even * can be coded in column 7 for comments.

Copyright © 2005, Infosys Technologies Ltd

43

ER/CORP/CRS/DB01/003 Version No:2.0a

Single Row Manipulation (1 of 6)
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.

Copyright © 2005, Infosys Technologies Ltd

44

ER/CORP/CRS/DB01/003 Version No:2.0a

Single Row Manipulation (2 of 6)
INSERT 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”.

Copyright © 2005, Infosys Technologies Ltd

45

ER/CORP/CRS/DB01/003 Version No:2.0a

Single Row Manipulation (3 of 6)
UPDATE EXEC SQL UPDATE EMPLOYEE SET SALARY = :WS-SALARY WHERE EMPNO = 10878 END-EXEC

Error Handling IF SQLCODE = 0 CONTINUE ELSE DISPLAY “UNIDENTIFIED ERROR OCCURRED”.

Copyright © 2005, Infosys Technologies Ltd

46

ER/CORP/CRS/DB01/003 Version No:2.0a

Single Row Manipulation (4 of 6)
DELETE EXEC SQL DELETE FROM EMPLOYEE WHERE EMPNO = :WS-EMPNO END-EXEC

Error Handling IF SQLCODE = 0 CONTINUE ELSE DISPLAY “UNIDENTIFIED ERROR OCCURRED”.

Copyright © 2005, Infosys Technologies Ltd

47

ER/CORP/CRS/DB01/003 Version No:2.0a

Single Row Manipulation (5 of 6)
Selecting Nullable columns(Columns which can take NULL values) EXEC SQL SELECT NAME, SALARY INTO :WS-NAME, :WS-SALARY:WS-SALARY-IND FROM EMPLOYEE WHERE EMPNO = :WS-EMPNO END-EXEC Indicator variable values and their meanings (1) 0 (2) -1 (3) -2 Not Null Null Exceptional condition

Copyright © 2005, Infosys Technologies Ltd

48

ER/CORP/CRS/DB01/003 Version No:2.0a

Single Row Manipulation (6 of 6)
Inserting Null values to columns 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”.

Copyright © 2005, Infosys Technologies Ltd

49

ER/CORP/CRS/DB01/003 Version No:2.0a

Cursors (1 of 7)
• Are memory structures used to handle multiple row selections at a time.

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.

Are defined in the WORKING- STORAGE SECTION/ PROCEDURE DIVISION.

Copyright © 2005, Infosys Technologies Ltd

50

ER/CORP/CRS/DB01/003 Version No:2.0a

Cursors (2 of 7)
Operations associated with a Cursor 2. DECLARE 4. OPEN 6. FETCH 8. CLOSE

Copyright © 2005, Infosys Technologies Ltd

51

ER/CORP/CRS/DB01/003 Version No:2.0a

Cursors (3 of 7)
Declaring a Cursor

Note: This is just the definition, DB2 executes this statement when we open it.

Copyright © 2005, Infosys Technologies Ltd

52

ER/CORP/CRS/DB01/003 Version No:2.0a

Cursors (4 of 7)
Opening a Cursor

EXEC SQL OPEN EMPCUR END-EXEC

Closing a Cursor

EXEC SQL CLOSE EMPCUR END-EXEC

Copyright © 2005, Infosys Technologies Ltd

53

ER/CORP/CRS/DB01/003 Version No:2.0a

Cursors (5 of 7)
Fetching results from a cursor

EXEC SQL FETCH EMPCUR INTO :WS-EMPNO,:WS-NAME,:WS-SALARY END-EXEC
• Remarks:
We get one row at a time. Only forward READ till SQLCODE = 100.

Copyright © 2005, Infosys Technologies Ltd

54

ER/CORP/CRS/DB01/003 Version No:2.0a

Cursors (6 of 7)
Fetching to Update

EXEC SQL DECLARE EMPCUR CURSOR SELECT EMPNO, NAME, SALARY FROM EMPLOYEE WHERE EMPNO > :WS-EMPNO FOR UPDATE OF SALARY END-EXEC
Note: This gives a U lock on the records.

Copyright © 2005, Infosys Technologies Ltd

55

ER/CORP/CRS/DB01/003 Version No:2.0a

Cursors (7 of 7)
Updating a row fetched from a cursor

EXEC SQL UPDATE EMPLOYEE SET SALARY = :WS-SALARY WHERE CURRENT OF EMPCUR END-EXEC
Note: This updates exactly one row even if we do not give the key value.

Copyright © 2005, Infosys Technologies Ltd

56

ER/CORP/CRS/DB01/003 Version No:2.0a

Summary
• • • • • • • SPUFI Hands On Introduction to Embedded SQL DCLGEN Hands On SQLCA Copy Book and its Use Single Row Manipulation Multiple Row Manipulation Cursors

Copyright © 2005, Infosys Technologies Ltd

57

ER/CORP/CRS/DB01/003 Version No:2.0a

Copyright © 2005, Infosys Technologies Ltd

58

ER/CORP/CRS/DB01/003 Version No:2.0a