Professional Documents
Culture Documents
DB2 Day - 04
DB2 Day - 04
Objectives
Sub Queries
Views, Alias and Synonym
Embedded SQL - Communicate with DB2
Describe set processing and handling cursors
Objectives
SELECT col1,col2,…,col N
FROM table
WHERE column =
(SELECT column1
FROM table
WHERE condition );
SUBQUERIES
SELECT ENAME
FROM EMP
WHERE SAL =
( SELECT MIN(SAL)
FROM EMP );
First it selects
MINIMUM SALRY
then employee
FNAME
View
All DML
ALTER and DROP of table
CREATE and DROP indexes
References – referential constraints
Run certain DB2 utilities
DCL (Data Control Language)
Syntax
GRANT <operation> On <Object Type> < object name>
This query will allow
TO <user id> / PUBLIC; SELECT operation
on EMPS & DEPTS
table for user called
DAVIN15 &
TRNR007
ALL means
Grant ALL ON TABLE EMPS TO USER1 all the
operations
Syntax
REVOKE <operations> ON <Object Type> <Object
name>
From user-id Now this revokes the
privilege from
TRNR007
He can’t perform
select from EMPS
table
Removing privileges on Table from the user
REVOKE SELECT
ON TABLE EMP,DEPTS FROM CMAP112;
EMPNO EMPNO
ENAME ENAME
UNION eliminates
duplicates .
Out off 6+3 = 9
records , 7 records
is displayed
Syntax
Static SQL
Program Preparation.
Embedded SQL programming
SQLWARNING
SQLERROR
SQLSTATE
SQL Codes
SQLCODE value
can be 0, positive
value or negative
SQLCODE MEANING values with
following meaning
0 Execution was successful
>0 Execution was successful with warning ( + ve )
E.g.
Record
EXEC SQL retrieved
from table
SELECT EMPNO, ENAME will be hold
in host
INTO :HV- variable
EMPNO, :HV-ENAME
FROM EMPS
WHERE EMPNO = :HV-EMPNO
END-EXEC.
Include Table
EMPPER and
SQLCA to get result
of DB2 transaction
SCODE is signed
variable to get
SQLCODE
SELECT col1,col2…
INTO :host-var Let us select record
FROM table-name for EMPNO 10014
WHERE Hence move this
value to variable Slight variation in
colname= :host-var Syntax
Check select
operation is
success / not with
SQLCODE = 0
else get the
information about
status with
SQLCODE When SQLCODE is 0,
display the retrieved record
else display SQLCODE
The procedure used to compile embedded SQL is
DSNHCOB1
The JCL as follows :
JCL is in the forms of two steps
Give the Name of Procedure
name of library in JCLLIB, Give the
Source PDS where you have name of
in DSNHCOB procedure DBRMLIB
PC.SYSIN PDS in
PC.DBRMLI
B
Give the
name of
DCLGEN
PDS in
PC.SYSLI
B
PLAN is
USERID
+P
Program
Name (Load
Module)
DBRM
Library
Where
DBRM’s
Do all these stored
changes &
Submit
RUN JCL
DB2 Pre-
Modified Source compiler
Non-SQL SQL
DBRM
Compilation
Plan
Link Edit
Load Module
Load Module Application plan
Execute
Pre-compilation
• INCLUDES SQLCA
EXEC SQL
WHENEVER SQLERROR GOTO <Err-Para>
END-EXEC.
EXEC SQL
WHENEVER SQLWARNING GOTO
<Warning-Para>
END-EXEC.
EXEC SQL
WHENEVER NOT FOUND CONTINUE
END-EXEC.