Professional Documents
Culture Documents
Contents
Contents...................................................................................................................................................................1
COBOL - COMMON BUSINESS ORIENTED LANGUAGE ..............................................................................3
DIVISIONS........................................................................................................................................................3
COBOL CODING SHEET..................................................................................................................................5
DATA TYPES....................................................................................................................................................6
LEVEL NUMBERS ...........................................................................................................................................6
SPECIAL LEVEL NUMBERS ...........................................................................................................................7
MOVE VERB ....................................................................................................................................................8
ADD VERB .......................................................................................................................................................9
SUBTRACT VERB .........................................................................................................................................10
MULTIPLY VERB ...........................................................................................................................................11
DIVIDE VERB .................................................................................................................................................11
COMPUTE VERB ...........................................................................................................................................12
REDEFINE CLAUSE ......................................................................................................................................12
IF STATEMENT ..............................................................................................................................................13
EVALUATE STATEMENT ..............................................................................................................................14
PERFORM STATEMENTS .............................................................................................................................14
EXIT, STOP RUN, EXIT PROGRAM AND GOBACK .....................................................................................17
SUB PROGRAMS...........................................................................................................................................18
TABLE HANDLING..........................................................................................................................................20
INTERNAL SORT............................................................................................................................................22
FILE HANDLING.............................................................................................................................................23
DB2 - DATABASE 2 .......................................................................................................................................28
COMPILATION PROCESS ............................................................................................................................36
INCLUDE and COPY statements....................................................................................................................39
1
IDENTIFICATION DIVISION
ENVIRONMENT DIVISION
DATA DIVISION
PROCEDURE DIVISION
CONFIGURATION SECTION gives the source and object computer details. i.e.
it gives the information about where the program has been writer(Terminal)
and where it can be executed. This section is optional now.
INPUT-OUTPUT SECTION gives the input and output files that the program is
going to be used. Whatever the files that we use in COBOL program that has
to be declared in the JCL.
DATA DIVISION: Its used to declare the variables used in the COBOL program, and to specify
the file attributes, and to pass the data from one program to another
program. It contains FILE SECTION, WORKING-STORAGE SECTION and
LINKAGE-SECTION.
LINKAGE SECTION - Its used to pass the data from main program to sub
program.
DATA DIVISION.
FILE SECTION.
FD CNTLCARD-FILE
BLOCK CONTAINS 0 RECORDS
RECORD CONTAINS 80 CHARACTERS.
LABEL RECORDS ARE STANDARD
DATA RECORD IS CNTLCARD-REC.
01 CNTLCARD-REC
PIC X(80).
WORKING-STORAGE SECTION.
77 RECORDS-READ
PIC 9(7)
77 RECORDS-SELECTED
PIC 9(7)
LINKAGE SECTION.
VALUE 0 COMP-3.
VALUE 0 COMP-3.
PROCEDURE DIVISION: Its used to write the logic of the program. The COBOL instructions start from
here. And also the execution of the program begins from the PROCEDURE
DIVISION.
IDENTIFICATION DIVISION.
PROGRAM-ID.
HELLOPGM.
AUTHOR.
PHANI.
INSTALLATION. WIPRO.
DATE-WRITTEN. 12-12-2005.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
A-START.
DISPLAY HELLOW WORLD OF MAINFRMES.
STOP RUN.
COBOL CODING SHEET
DATA TYPES
LEVEL NUMBERS
Ex6
01 WORK-REF-NUM.
05 WORK-REF-NUM-1
05 WORK-REF-NUM-2
05 FILLER
05 WORK-REF-DATE
05 FILLER
05 WORK-REF-19-23
PIC
PIC
PIC
PIC
PIC
PIC
01 WS-NAME.
05 INITIAL
05 SUR-NAME
05 FIRST-NAME
PIC X(1)
PIC X(1)
PIC X(1)
X(1)
X(2)
X(4)
9(4)
X(7)
X(5)
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
SPACES.
SPACES.
SPACES.
ZERO.
SPACES.
SPACES.
VALUE SPACES.
VALUE SPACES.
VALUE SPACES.
Examples
PICTURE 999 -> Stores a 3 digit +ve no.
PICTURE S999 -> Stores a 3 digit +ve/-ve no.
PICTURE XXXX -> Stores a string of 4 characters
PICTURE 99V99 -> Stores a +ve real from 0-99.99
PICTURE S9V9 -> Stores a +ve/-ve real from 9.9 to +9.9
Shorthand Notation : You can abbreviate 9999 as 9(4), XXXX as X(4), 999V99 as
9(3)V9(2), S99999 as S9(5) etc.
VALUE Clause : We can assign an initial value to the elementary dataitems/variables by using the VALUE clause. It is an optional clause.
01 WS-EMP-SALARY
01 WS-EMP-JDATE
PIC 9(4)V99
PIC X(10)
VALUE 1000.52
VALUE 26-06-09
PIC X(1)
PIC X(15)
PIC X(20)
VALUE SPACES.
VALUE SPACES.
VALUE SPACES.
7
RECORD-2.
10 FIELD-C
10 FIELD-D
77 - This level number is used for declaring the individual items. That means there
will be no sub-levels for that variable.s
77
77
INITIAL
SUR-NAME
PIC X(1)
PIC X(15)
VALUE SPACES.
VALUE SPACES.
STATUS-IND
88 GOOD-STATUS
88
88
88
END-OF-FILE-STATUS
DUP-REC-STATUS
NO-RECORD-FOUND
PIC XX
VALUE SPACES.
VALUES '00'
'97'.
VALUE '10'.
VALUE '22'.
VALUE '23'.
MOVE VERB
MOVE verb is used to move the values from one variable to another variable.
The way MOVE statement work is different for NUMERIC and ALPHABETIC,
ALPHA-NUMERIC variables.
There are different MOVE statements.
Format-1:8
MOVE 123 TO A.
MOVE A TO B.
X(15).
9(3).
9(5)V9(2).
01 EMP-REC-2.
05 NAME
05 AGE
05 SALARY
05 ADDR
X(15).
9(3).
9(5)V9(2).
X(30).
PIC
PIC
PIC
PIC
O\P - 25
15
-10 ==> Here A & B values are not changed and result stored in C
MULTIPLY VERB
01 C PIC
01 D PIC
9(3).
9(3).
For doing large arithmetic operations we cant use the ADD, SUBTRACT,
MULTIPLY and DIVIDE verbs.
So for doing this we can use COMPUTE verb. And also we can use it for
assigning a value to a variable.
ExCOMPUTE A = 100.
COMPUTE A = (B+C)/E.
REDEFINE CLAUSE
REDEFINE clause is used to redefine the same memory location with different
names and also with different data types.
Ex:01
WS-AUG28-TODAY-DATE
PIC 9(7).
01
01
WS-CARD-NUMBER
PIC X(16).
01
IF STATEMENT
13
EVALUATE STATEMENT
EVALUATE statement is also used for decision making in COBOL. This is like a
CASE statement in C language.
Format-1:ExEVALUATE TRUE
WHEN SQLCODE = 0
DISPLAY DB2 QUERY SUCCESSFULLY COMPLETED
WHEN SQLCODE = 100
DISPLAY ROW NOT FOUND
WHEN SQLCODE = -811
DISPLAY MULTIPLE ROWS FOUND
WHEN OTHER
DISPLAY UNKNOWN ERROR WHILE EXECUTING THE TRANSACTION
END-EVALUATE.
Format-2:ExEVALUATE SQLCODE
WHEN 0
DISPLAY DB2 QUERY SUCCESSFULLY COMPLETED
WHEN 100
DISPLAY ROW NOT FOUND
WHEN -811
DISPLAY MULTIPLE ROWS FOUND
WHEN OTHER
DISPLAY UNKNOWN ERROR WHILE EXECUTING THE TRANSACTION
END-EVALUATE.
PERFORM STATEMENTS
Format-1: - In this format the control will go to the specified paragraph and
executes all the statements mentioned in that and returns back to the statement
after the PERFORM.
It calls the Paragraph only one time in this case.
PERFORM B-CALCULATE-SALARY.
14
Format-2: - In this format the control will go to the specified paragraph and
executes all the statements mentioned in that UNTIL a specified number of times
and then returns back to the statement after the PERFORM.
It calls the Paragraph 10 Times.
PERFORM B-CALCULATE-SALARY UNTIL 10 TIMES.
Format-3: - In this format the control will go to the specified paragraph and
executes all the statements mentioned in that UNTIL a specified condition is TRUE.
It calls the Paragraph until a specified condition is TRUE.
PERFORM B-CALCULATE-SALARY UNTIL UNTIL END-OF-FILE = YES.
Format-4: - In this format the control will go to the specified paragraph and
executes all the statements mentioned in that UNTIL a specified condition is TRUE.
Here it will maintain a counter and increments it to the value specified for each
iteration.
PERFORM B-CALCULATE-SALARY VARYING CTR FROM 1 BY 1 UNTIL CTR > 10.
In this example the CTR starts from 1 and for each iteration it will
increment with value 1. (BY 1).
15
Ex-1:PROCEDURE DIVISION.
100-MAIN-PARA.
DISPLAY START OF PROGRAM.
PERFORM 200-INDIA-PARA.
DISPLAY END OF PROGRAM.
STOP RUN.
200-INDIA-PARA.
DISPLAY INDIA .
O\P INDIA
Ex-2:PROCEDURE DIVISION.
100-MAIN-PARA.
DISPLAY START OF PROGRAM.
PERFORM 200-INDIA-PARA UNTIL 3 TIMES.
DISPLAY END OF PROGRAM.
STOP RUN.
200-INDIA-PARA.
DISPLAY INDIA .
O\P INDIA
INDIA
INDIA
Ex-3:PROCEDURE DIVISION.
100-MAIN-PARA.
DISPLAY START OF PROGRAM.
PERFORM 200-INDIA-PARA THRU 400-USA-PARA UNTIL WS-FLAG = YES
DISPLAY END OF PROGRAM.
STOP RUN.
200-INDIA-PARA.
DISPLAY INDIA .
300-UK-PARA.
DISPLAY UK .
400-USA-PARA.
DISPLAY USA .
MOVE YES TO WS-FLAG.
O\P INDIA
UK
USA
16
PROGRAM AND GO BACK: This statement is also used to indicate its end of the program.
Must be used in the Sub-programs.
Control will goes back to the main program from where its
called.
PROCEDURE DIVISION.
100-MAIN-PARA.
DISPLAY HELLOW IBM MAINFRAMES.
EXIT PROGRAM.
100-EXIT.
EXIT.
17
SUB PROGRAMS
Changes in the Sub-Program:To receive the data passed from main program, the sub-program
has to make the below changes.
o We need to define the No of arguments that we want to
receive in the LINKAGE SECTION of the Sub-program.
LINKAGE SECTION.
18
01 DATA.
05 LS-A
05 LS-B
05 LS-C
PIC 9(2).
PIC 9(2).
PIC 9(2).
2. DYNAMIC CALL
01
STATE-TAB.
05 DIST-NAME PIC X(3) OCCURS 24 TIMES.
The above example defines 1-D table where it can store up to 24 rows where is
row size is 3 bytes.
Total Size - 3 * 24 ==> 72 bytes
How to define the TWO-DIMENSTIONAL ARRAYS:01
STUDENT-TAB.
05 NAME
10 SUBJECT
PIC X(20)
PIC 9(3)
OCCURS 30 TIMES.
OCCURS 6 TIMES.
In the above example there 30 students in the class and for each
Student there are 6 subjects.
How to define the ONE-DIMENSTIONAL ARRAYS DYNAMICALLY:01
STATE-TAB.
05 DIST-NAME PIC X(3) OCCURS 1 TO 100 TIMES DEPENDING ON WS-VAL.
to define the TABLE with INDEX option:INDEX is used to create the INDEX in the table.
Then INDEX is used for checking or retrieving the records in a faster manner.
We need to set the INDEX variable to 1.
To increment or decrement the value of the INDEX variable we need to use
SET UPBY or SET DOWNBY keywords.
We dont need to declare the index variables separately in the WORKINGSTORAGE SECTION.
01
STATE-TAB.
05 DIST-NAME PIC X(5) OCCURS 24 TIMES INDEX BY IDX.
If we do the SORTING in the COBOL program then its called the INTERNAL
sorting.
If we do the sorting in the JCL using the utility SORT then its called the
EXTERNAL sort.
For sorting we need to define the SORT work files in the JCL.
The SORT work files will be represented in the COBOL program using the SD
SD - SORT DESCRIPTION
For doing the SORT we required 3 files.
o Sort work file This file is required if the input file contains huge
records to sort.
o Input file
o Output file
FILE HANDLING
All the files used in the JCL needs to be declared in the ENVIRONMENT
DIVISION and also in the DATA DIVISION.
Follow the below steps in order to use the files in the COBOL program.
1. Declare the files in the INPUT-OUTPUT SECTION of ENVIRONMENT
DIVISION.
2. Declare the file attributes in the FILE SECTION of DATA DIVISION.
3. OPEN the file in one off the modes (INPUT, OUTPUT, I-O, EXTEND)
4. Use one off the statements (READ, WRITE, REWRITE, DELETE)
5. Close the file using the statement CLOSE.
PS FILE: In PS file, the records will be organized in Sequential order and accessed in
Sequential order only. So we need to mention ORGANIZATION and ACCESS
MODE as SEQUENTIAL.
After every operation on the file, OS will places the status code in the
variable declared using FILE STATUS.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO INDD
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-IN-FILE-STAT.
KSDS FILE: In KSDS file, the records will be organized based on the Index order and
accessed Sequentially or Dynamically or Randomly. Hence mention the
below values for KSDS.
ORGANIZATION IS INDEXED
ACCESS MODE IS SEQUENTIAL\RANDOM\DYNAMIC
And also you need to mention the Key as below.
23
Ex-1
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO KSDS-DD
ORGANIZATION IS INDEXED
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-IN-FILE-STAT.
Ex-2
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO KSDS-DD
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
FILE STATUS IS WS-IN-FILE-STAT.
Ex-3
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO KSDS-DD
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
FILE STATUS IS WS-IN-FILE-STAT.
ESDS FILE: In ESDS file, the records will be organized based on the Entry and accessed
sequentially. Hence mention the below values for ESDS.
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
To differentiate the PS file and ESDS file you need to put AS- before the
DDNAME.
Ex-1
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO AS-ESDS-DD
ORGANIZATION IS SEQUENTIAL
24
INPUT
OUTPUT
I-O
EXTEND
IN-FILE
OUT-FILE
IN-FILE
IN-FILE
IF WS-IN-FILE-STAT = 00
CONTINUE
ELSE
DISPLAY ERROR IN OPENING THE FILE
END-IF.
TO
EOF
WS-IN-EID
WS-IN-ENAME
WS-IN-ADDR
WS-IN-SALARY
WS-IN-DEPTNAME
WRITE OUT-REC
TO EOF-IN
TO
TO
TO
TO
TO
WS-OUT-EID
WS-OUT-ENAME
WS-OUT-ADDR
WS-OUT-SALARY
WS-OUT-DEPTNAME
FROM WS-OUT-REC.
300-EXIT.
EXIT.
400-CLOSE-FILES.
CLOSE IN-FILE, OUT-FILE.
400-EXIT.
EXIT.
27
DB2 - DATABASE 2
Indexes
Triggers
Views
Stored Procedures
Storage Group: A Storage Group is a group of volumes on DASD (direct access storage
device). The Volumes hold the data sets in which tables and indexes are
actually stored.
29
In IBM-DB2 the table has to be stored in the Table space, where the table
space is stored in the Database.
FK
DEPTNAM MGRN
E
O
PAYROLL
30
FINANCE
60
ADMIN
-----
PK
EMPNO
LASTNAM
E
FK
DEPTN
O
ROLE
31
30 KUMAR
200 BROWN
340 JOHN
C01
D11
E21
MGR
SE
SSE
ALTER - this command is used to alter the table, table space and other objects.
ALTER TABLE EMPLOYEE
DROP - This command is used to drop the table. When we drop the table all the
rows and columns of it will be automatically deleted.
DROP TABLE EMPLOYEE
DROP INDEX EMPIDX
DATA CONTROL LANGUAGE - This is used for controlling the access on the db2
objects.
GRANT, REVOKE are the commands used in this language.
GRANT - To give the access to a particular user id or to all the users on the db2
objects we use this command.
GRANT ON TABLE EMPLOYEE ALL TO PUBLIC This will give all the accesses to all
the users.
GRANT ON TABLE EMPLOYEE SELECT, INSERT TO FSS117 This will give the
SEELCT, INSERT access only to the user id FSS117.
REVOKE - Is used to revoke the access.
DATA MANIPULATION LANGUAGE - This is used for controlling the data stored
on the database.
1.
2.
3.
4.
SELECT
INSERT
UPDATE
DELETE
32
SELECT - SELECT statement is used to get all the data or required data from the
tables.
Types of SELECT statements:SELECT * FROM EMPLOYEE This will return all rows & columns from the
EMPLOYEE table
2. SELECT EMPID, EMPNAME FROM EMPLOYEE This will return only the
columns EMPID, EMPNAME from EMPLOYEE table.
3. SELECT DISTINCT(DEPTNAME) FROM DEPT This will eliminate the duplicate
rows from DEPTNAME column and display the result.
1.
WHERE clause - The WHERE clause is used for checking the condition before
retrieving the results from the table.
SELECT EMPID, EMPNAME
FROM EMPLOYEE
WHERE EMPID = 1001;
1001
You can also use any logical operator (AND, OR, NOT)
SELECT EMPID, EMPNAME
FROM EMPLOYEE
WHERE EMPID = 1001 AND EMPID = 1002;
EMPID - 1001 & 1002.
BETWEEN Operator - This will check the range of values and display only the
matched rows.
SELECT EMPID, EMPNAME, SALARY
FROM EMPLOYEE
WHERE EMPID BETWEEN 1001 AND 1100 This will retrieve all the rows where
EMPID is in between 1001 to 1100.
IN Operator - This will check the list of values and display only the matched
rows.
SELECT EMPID, EMPNAME, SALARY
FROM EMPLOYEE
33
WHERE EMPID IN (1001,1003,1010,1100) This will retrieve all the rows where
EMPID is in the list 1001, 1003, 1010, 1100.
LIKE Operator - LIKE operator is used with the character data types and is used
to check for the wild card strings.
If we want find the rows where the EMPNAME starts with KUMAR.
SELECT EMPID, EMPNAME, SALARY
FROM EMPLOYEE
WHERE EMPNAME LIKE KUMAR% This will retrieve all the rows where
EMPNAME starts with the string KUMAR
Ex1 - Retrieve all the rows where 3rd character of the EMPNAME is M.
SELECT EMPID, EMPNAME, SALARY
FROM EMPLOYEE
WHERE EMPNAME LIKE __M% Here first two characters are (_) and then
written the 3rd character as M and then %.
DB2 Column functions Below are the most common used DB2 column functions.
1. COUNT
2. AVG
3. MAX
4. MIN
5. SUM
COUNT - This function is used to count the number of rows in a db2 table.
SELECT COUNT(*) FROM EMPLOYEE This will return the number of rows
(Including duplicate)
SELCT COUNT(DISTINCT *) FROM EMPLOYEE This will return the number of rows
(Excluding duplicate)
AVG - This function is used to find the average value of a column.
SELECT AVG(SALARY) FROM EMPLOYEE This will return the average salary of
each employee
34
The WHERE clause selects rows before grouping and HAVING clause selects
rows after grouping.
The WHERE clause can be used without GROUP BY clause and HAVING has to
be used with the GROUP BY clause.
INSERT - INSERT statement is used to insert the rows into the DB2 table.
35
The INSERT using SELECT form is used to insert one or more rows into
the table or view using values from other tables or views.
The INSERT using n ROWS form is used to insert multiple rows into the
table or view using the values provided in a host-structure-array.
If a row is inserted into the DB2 table you will get the SQLCODE = 0 to indicate it
has inserted successfully.
If SQLCODE = -803 then it means a duplicate record exist.
UPDATE - UPDATE statement is used to update the columns of the DB2 table.
UPDATE EMPLOYEE
SET SALARY = 10000
WHERE EMPID = 1001; This will set the salary to 10000 for the EMPID 1001
UPDATE EMPLOYEE
SET SALARY = SALARY + 1000 This will increment the salary of all the
employees to 10000.
DELETE - DELETE statement is used to delete the rows of the DB2 table.
DELETE FROM EMPLOYEE WHERE EMPID = 1001 Deletes only one row.
DELETE FROM EMPLOYEE Deletes all the rows from Employee table.
COMPILATION PROCESS
The compilation process for COBOL-DB2 program includes various steps.
1. PRE COMPILATION
36
2.
3.
4.
5.
COMPILATION
LINK EDIT
BIND
EXECUTE
PRE COMPILATION In this process the compiler checks for the all the SQL statements which are
embedded within the EXEC SQL and END-EXEC statements and puts into a
module called as DBRM.
And converts all the SQL statements into COBOL CALL statements.
And it places a TIME STAMP on the DBRM module.
COMPILATION It will do a normal Compilation
BIND - BIND reads the sql statements from DBRM and produces the access path
logic to retrieve the data from DB2.
It contains two types.
1. PACKAGE
2. PLAN.
PACKAGE A PACKAGE takes DBRM as input and produces a single package containing
the optimized access path logic.
The Package then can be bound into Plan using BIND PLAN command.
A Package cant be executable so we need to have the PLAN to execute a
Cobol-DB2 program.
The advantage of the PACKAGE is if one of the program is changed instead of
binding the entire plan will just BIND the impacted program. So it will save
the binding time and cost.
PLAN A PLAN take one or more DBRMs or one or more PACKAGEs as input and
produces the application plan.
A PLAN is required to execute the COBOL-DB2 program.
Parameters used in the BIND card BIND PLAN(FSS106) MEMBER(CRC210A0) ACTION(REP) ACQUIRE(USE) RELEASE(COMMIT) 37
ISOLATION(CS) OWNER(FSS106)
PLAN name - Here in Training we give the plan name as the user id.
MEMBER - Your DBRM name has to give here. Nothing but your program name.
ACTION - You can use two options here
ADD - To add the member name into the PLAN list.
REP - To replace the already existing member name in the PLAN list.
ACQUIRE and RELEASE These two options of BIND will determine when to lock the db2 object (Table,
Partition or Tablespace) and when to release the lock on the db2 object.
ACQUIRE(ALLOCATE) - Acquires the lock when the DB2 object is allocated. This
option is not allowed when its BIND or REBIND.
ACQUIRED(USE) - Acquires the lock when the object is first accessed.
RELEASE(DEALLOCATE) - Releases the lock when the object is deallocated i.e.
when your program execution is completed.
RELEASE(COMMINT) - Releases the lock when there is an explicit COMMIT
statement in your program.
ISOLATION LEVELS The ISOLATION option on the BIND card determines the types of locking the table.
There are four types of ISOLATION levels.
1.
2.
3.
4.
CURSOR STABILITY
READ STABILITY
REPEATABLE READ
UNCOMMITED READ
1. CURSOR STABILITY - In this DB2 locks the table on the page wise and this is the
default lock. Once the page scan is done then it will release the lock and then
move to the next page.
2. READ STABILITY - In this DB2 locks each row on the table and does not allow any
process until the lock is released.
3. REPEATABLE READ - In this DB2 locks all the rows of the table and does not
allow any INSERT, DELETE, UPDATE operations from other processes until the first
process is end.
38
4. UNCOMMITED READ - In this DB2 locks all the rows of the table and does not
allow any other processes until an explicit commit is done.
EXEC SQL
INCLUDE SQLCA
END-EXEC.
JOINS
Sometimes you may required to retrieve some columns from TABLE-1 and some
columns of TABLE-2. In this case you will go for the JOIN to combine two tables and
retrieve the required columns.
You need to write the JOIN in the FROM clause of SELECT statement.
NAME
PHANI
KUMAR
INDRA
REDDY
MAHESH
RAGHU
RAGHAV
SALARY
30000
45000
22000
48000
12300
26300
87564
DEPTNO
D001
D001
D002
D002
D001
D003
D003
39
D002
D003
D001
----
DEPTNAME
SOFTWARE
PAYROLL
FINANCE
TRANSPORT
INNER JOIN In this it will give only the rows that match the condition.
SELECT A.ENO, A.NAME, B.DEPTNAE
FROM EMPLOYEE A, DEPT B
WHERE A.DEPTNO = B.DEPTNO
OR
SELECT A.ENO, A.NAME, B.DEPTNAE
FROM EMPLOYEE A INNER JOIN DEPT B
WHERE A.DEPTNO = B.DEPTNO
O\P
ENO
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
NAME
PHANI
KUMAR
INDRA
REDDY
MAHESH
RAGHU
RAGHAV
RAVI
MANOJ
SIMHA
DEPTNAME
SOFTWARE
SOFTWARE
PAYROLL
PAYROLL
SOFTWARE
FINANCE
FINANCE
PAYROLL
FINANCE
SOFTWARE
NOTE - It has display only the 10 rows of EMPLOYEE table and has not
displayed ENO - 1011 as there is no department like ---- in DEPT
table.
LEFT OUTER JOIN 40
In this it will give only the rows that match the condition and also
the non matching rows from the left side of the table.
SELECT A.ENO, A.NAME, B.DEPTNAE
FROM EMPLOYEE A LEFT OUTER JOIN DEPT B
WHERE A.DEPTNO = B.DEPTNO
ENO
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
NAME
PHANI
KUMAR
INDRA
REDDY
MAHESH
RAGHU
RAGHAV
RAVI
MANOJ
SIMHA
KISHORE
DEPTNAME
SOFTWARE
SOFTWARE
PAYROLL
PAYROLL
SOFTWARE
FINANCE
FINANCE
PAYROLL
FINANCE
SOFTWARE
----
1011 - Is a non matching row but still we got it in the output and the reason for this
is LEFT OUTER JOIN returns the non matching rows also from the left side of the
table.
RIGHT OUTER JOIN In this it will give only the rows that match the condition and also
the non matching rows from the right side of the table.
SELECT A.ENO, A.NAME, B.DEPTNAE
FROM EMPLOYEE A RIGHT OUTER JOIN DEPT B
WHERE A.DEPTNO = B.DEPTNO
ENO
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
----
NAME
PHANI
KUMAR
INDRA
REDDY
MAHESH
RAGHU
RAGHAV
RAVI
MANOJ
SIMHA
----
DEPTNAME
SOFTWARE
SOFTWARE
PAYROLL
PAYROLL
SOFTWARE
FINANCE
FINANCE
PAYROLL
FINANCE
SOFTWARE
TRANSPORT
41
FULL OUTER JOIN In this it will give only the rows that match the condition and also
the non matching rows from the left side of the table and then right
side of the table.
SELECT A.ENO, A.NAME, B.DEPTNAE
FROM EMPLOYEE A FULL OUTER JOIN DEPT B
WHERE A.DEPTNO = B.DEPTNO
ENO
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
----
NAME
PHANI
KUMAR
INDRA
REDDY
MAHESH
RAGHU
RAGHAV
RAVI
MANOJ
SIMHA
KISHORE
----
DEPTNAME
SOFTWARE
SOFTWARE
PAYROLL
PAYROLL
SOFTWARE
FINANCE
FINANCE
PAYROLL
FINANCE
SOFTWARE
---TRANSPORT
42
UNION & UNION ALL This is used to merge the rows of same table or different tables.
It works based on the rows.
When you use the UNION the column names in both queries should have
same name and should contain same number of columns.
UNION will eliminate the duplicate rows.
SELECT EMPNO
FROM DSN8810.EMP
WHERE WORKDEPT = 'D11'
UNION ALL
SELECT EMPNO
FROM DSN8810.EMPPROJACT
WHERE PROJNO = 'MA2112' OR
PROJNO = 'MA2113' OR
PROJNO = 'AD3111'
ORDER BY EMPNO;
COMMIT & ROLLBACK options COMMIT - This option is used to commit the work done. That means db2 saves the
data what ever is inserted, updated, deleted till that point of time.
EXEC SQL
COMMIT
END-EXEC.
ROLLBACK - This option is used to not save the work done. Opposite of COMMIT.
EXEC SQL
ROLLBACK
END-EXEC.
43
44