Академический Документы
Профессиональный Документы
Культура Документы
Agenda
yIntroduction
yBasic SQL within a HLL program
yProcessing multiple records
yError detection
yDynamic SQL
yTips and Techniques
ySummary
What is SQL?
SQL - Structured Query Language
yData language for manipulation of a Relational database
yEnglish keyword-oriented
yExcellent tool for application development environment
Query
Data definition
Data manipulation
Data control
yPowerful language for Set-At-A-Time processing
SQL is NOT an end user query product!
V5 SQL Support...
Part 1: DB2 Database Manager
yIncluded with IBM i (operating system)
V5: 5722-SS1
ySQL parser and run time support
SQL license (5722-ST1) not required to run SQL
applications
Support for compiled programs using embedded SQL
ySQL APIs
QSQPRCED - Provides extended dynamic SQL capability
QSQCHKS - Provides syntax checking for SQL
statements
yX/Open SQL Call Level Interface
yV5R1 - SQL Statement Processor
RUNSQLSTM Command
V5 SQL Support
Part 2: DB2 for IBM i Query Manager & SQL Development Kit
yProgram number 5722-ST1
yQuery Manager
yInteractive SQL
ySQL precompilers
Required for embedding SQL in HLL programs
More later
ySQL REXX interface
Agenda
yIntroduction
yBasic SQL within a HLL program
yProcessing multiple records
yError detection
yDynamic SQL
yTips and Techniques
ySummary
yRPG
yCOBOL
yC
yJava
yAS/400 PL/I
yFORTRAN/400
User
Source
File
Syntax check
X-ref host variables
SQL statements to calls
Comment SQL statements
Language
Compiler
Modified
Source
File
Processed
SQL
Statements
Program
Access
Plan
SQL Commands
Basic commands
ySELECT - retrieves data; one row or multiple
yUPDATE - updates one row or multiple
yDELETE - deletes one row or multiple
yINSERT - adds one row or multiple
Additional functions for complex processing
yDECLARE CURSOR builds temp result table
yOPEN and CLOSE open/close result table
yFETCH row retrieval
yCOMMIT and ROLLBACK journaling functions
yGRANT and REVOKE security functions
10
1
6
31
50 EMPNBR
30 NAM
32 DEPT
13
F3=Exit
F4=Prompt
F16=Repeat find
F5=Refresh
F9=Retrieve
F17=Repeat change
F10=Cursor
F24=More keys
14
.
.
.
.
.
.
.
.
.
.
.
.
.
.
emp_______________________________
nbr,_nam,_sal_____________________
dpt_=_:dept_______________________
__________________________________
__________________________________
__________________________________
sal_______________________________
Bottom
F4=Prompt
F12=Cancel
F5=Refresh
F14=Delete
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
__________
N
Y=Yes, N=No
N
Y=Yes, N=No
N
Y=Yes, N=No
F6=Insert line
F15=Split line
15
F9=Subquery
F24=More keys
2012 IBM Corporation
16
F5=Refresh
F9=Retrieve
F17=Repeat change
F10=Cursor
F24=More keys
SQL Statement
Start of SQL statement
17
18
RPG
COBOL
I
empds
DS
I
I
I
C*
C/EXEC SQL
C+
SELECT nbr, nam, dpt
C+
INTO :empds
C+
FROM emp
C+
WHERE nbr = :empnbr
C/END-EXEC
1
6
31
50 EMPNBR
30 NAM
31 DEPT
19
Agenda
yIntroduction
yBasic SQL within a HLL program
yProcessing multiple records
yError detection
yDynamic SQL
yTips and Techniques
ySummary
20
10
21
11
12
OPEN Statement
empcsr
25
FETCH Statement
Two Functions
yPosition the cursor for the next operation
C*
C/EXEC SQL
C+
C+
FETCH
C+
C/END-EXEC
NEXT
FROM
empcsr
26
13
Positions Cursor
On the next row after the current row
On the row before the current row
On the first row
On the last row
Before the first row - must not use INTO
After the last row - must not use INTO
On the current row (no change in position)
Relative n
27
28
14
CLOSE Statement
Closes the cursor
Cursor must be opened in order to be closed
DB2 for i5/OS closes cursors for other reasons also:
job end
activation group ends
program ends
modules ends
commit or rollback without a 'with hold' clause
error handling......
Rule of thumb - close the cursor when finished with it
C*
C/EXEC SQL
C+
C+
CLOSE
C+
C/END-EXEC
30
empcsr
15
16
Agenda
yIntroduction
yBasic SQL within a HLL program
yProcessing multiple records
yError detection
yDynamic SQL
yTips and Techniques
ySummary
33
SQL
INCLUDE SQLCA
END-EXEC
34
17
Char(8)
Integer
Integer
SmallInt
Char(70)
Char(8)
Array of Integers
35
36
SQLWarn
SQLWarn0
Char(11)
Char(1)
SQLWarn1
Char(1)
SQLWarn2
SQLWarn3
Char(1)
Char(1)
SQLWarn4
Char(1)
SQLWarn5
SQLWarn6
SQLWarn7
SQLWarn8
Char(1)
Char(1)
Char(1)
Char(1)
SQLWarn9
SQLWarnA
SQLState
Char(1)
Char(1)
Char(5)
18
SQLCODE Values
SQLCODE (SQLCOD) contains return code
= 0 Successful statement execution
> 0 Successful, with warning condition
< 0 Unsuccessful - statement failed
SQLCODE value indicates exact error or condition
e.g. 100 = Row not found (or end of file)
e.g. -552 = Not authorized to object
SQLCODE values have corresponding messages
e.g. SQL0100 = Row not found
e.g. SQL0552 = Not authorized to &1.
37
COBOL
38
C/EXEC SQL
C+ SELECT name INTO :nam
C+
WHERE emp = 100
C/END-EXEC
C
SQLCOD
IFLT
C
C
C
SQLCOD
IFGT
C
C
0
EXSR
ENDIF
0
EXSR
ENDIF
ERR
NF
EXEC SQL
SELECT name INTO :lastname
WHERE emp = 100
END-EXEC.
IF SQLCODE < 0
PERFORM ERROR-ROUTINE.
ELSE
IF SQLCODE > 0
PERFORM NOT-FOUND-ROUTINE
ELSE
PERFORM GOOD-REC-ROUTINE.
2012 IBM Corporation
19
20
Retrieve
C*
C/EXEC SQL
C+
OPEN empcsr
C/END-EXEC
C*
C
IF SQLCODE <> 0
C
EXSR Err
C
DOU = 100
C*
C/EXEC SQL
C+
FETCH NEXT FROM empcsr
C+
INTO :number, :name, :salary
C/END-EXEC
C*
C
IF SQLCODE <> 0
NOTE: Not all
C
EXSR Err
program logic is
C
DOU = 100
C*
shown!!
41
Process
Close
C*
C/EXEC SQL
C+
UPDATE emp
C+
SET sal = sal + :raise
C+
WHERE CURRENT OF empcsr
C/END-EXEC
C*
C
IF SQLCODE <> 0
C
EXSR Err
C
ENDIF
C
ENDIF
C
ENDDO
C*
C/EXEC SQL
C+
CLOSE empcsr
NOTE: Not all
C/END-EXEC
program logic is
shown!!
42
21
Agenda
yIntroduction
yBasic SQL within a HLL program
yProcessing multiple records
yError detection
yDynamic SQL
yTips and Techniques
ySummary
43
Dynamic SQL
What is Dynamic SQL?
Dynamic SQL is a different way to use SQL
SQL statements are not pre-defined in program
Dynamically created on the fly as part of
program logic
SQL pre-compiler cannot fully process dynamically
created SQL statements
PREPARE statement used in program logic to
compile dynamically created SQL statements
Can be used with SQL EXECUTE statement
44
22
Dynamic SQL
What is Dynamic SQL?
Offers a high degree of application flexibility
Can create/build SQL statement
Based on parameters received from
Interactive user interface
List selection techniques
Application control file
Use any programming language
45
Dynamic SQL
46
23
Dynamic SQL
Example 2: Dynamically select records from the Employee
Master File - Any fields, records, or sequence
C* User prompted to enter delete condition (InpCnd)
C*
C/EXEC SQL
C+
C+
PREPARE search FROM :sqlstm
C+
C/END-EXEC
C*
C/EXEC SQL
C+
C+
DECLARE empcsr CURSOR FOR search
C+
C/END-EXEC
C*
C/EXEC SQL
C+
C+
OPEN empcsr
C+
C/END-EXEC
48
24
Dynamic SQL
Performance considerations
yDynamic SQL can be resource intensive
yRemember that a dynamic SQL statement is parsed
(interpreted) and executed within the application program
from which it is called
49
Agenda
yIntroduction
yBasic SQL within a HLL program
yProcessing multiple records
yError detection
yDynamic SQL
yTips and Techniques
ySummary
50
25
Performance Tips
Embedded SQL Tips
Test statements in Interactive SQL before embedding them
When exiting Interactive SQL session
You can save session statements to a source member
Then copy from this member into your program source
Default for SQL is to use Commitment Control
Requires journaling
Program execution fails if updated files are not journaled
To request no commitment control
COMMIT(*NONE) on compile
SQL precompile step happens before RPG/COBOL compile
Therefore, if SQL syntax or semantic error occurs, no "typical"
compile source listing available
Can be very difficult to work through problems at this stage
51
Performance Tips
Designing and Writing Efficient SQL Queries
SQL uses two basic ways to retrieve data
Dataspace scan or arrival sequence Generally used
when MORE than 20% of records will be selected
Index based or keyed access Generally used when
LESS than 20% of records will be selected
If SQL can use an index, performance can improve
significantly!
Create indexes for columns frequently referenced in
WHERE clause
GROUP BY clause
ORDER BY clause
Create indexes for fields that are frequently used to join
files
52
26
Performance Tips
Performance Analysis Tools
PRTSQLINF
Prints information about the embedded SQL
statements in a program, SQL package or service
program
SQL statements
Access plans
List of command parameters used by precompiler
STRDBMON
Predictive Query Governor
CHGQRYA command
TRCJOB
Visual Explain (first in V4R5)
Operations Navigator
53
Performance Tips
Multiple Row FETCH
Based on host structure
RPG - Multiple Occurence Data Structure
COBOL - Occurs clause on declaration of the group item
Clause on FETCH
FOR n ROWS (n = number of rows to be returned)
Specifies number of rows to be retrieved to fill the structure
D
EMP
DS
D
NBR
D
NAME
D
JOB
C*
C
Z-ADD
C/EXEC SQL
C+
FETCH Next
C+
FROM CustomerCursor
C+
FOR 10 ROWS
C+
INTO Emp
C/END-EXEC
C
Eval
C*
54
5
25
1
5
Occurs(10)
0
JOB
ErrCond = SQLErrD(5)
2012 IBM Corporation
27
Performance Tips
Multiple Row FETCH - continued
yFirst row fetched- placed into first element of host structure
yFetching stops when n rows are returned - or no more records
yProgram variable may be used to specify number of records to
be fetched (Note: RPG IV - %Elem built in function)
yBe Careful - Fetching is always forward from position set by
positioning keyword
FETCH RELATIVE -3 .... FOR 3 ROWS
is not the same as
FETCH PRIOR .... FOR 3 ROWS
yResults:
SQLCA updates SQLErrCode(3) to reflect # rows retrieved
If no rows returned and no other exceptions, SQLCode is
100
If row(s) returned contains last available row,
SQLErrCode(5) set to 100
55
Performance Tips
Blocked INSERT
Multiple elements from a host structure are inserted
into a table
records to be inserted
Executes as if n INSERT statements had been issued
but with improved performance
D
EMP
DS
D
NBR
5
D
NAME
25
D
JOB
C*
C/ EXEC SQL
C+
INSERT INTO Customer
C+
10 ROWS
C+
VALUES :Emp
C/ END-EXEC
56
Occurs(10)
0
1
28
57
Agenda
yIntroduction
yBasic SQL within a HLL program
yProcessing multiple records
yError detection
yDynamic SQL
yTips and Techniques
ySummary
58
29
Summary
Practical, effective solution for applications
flexibility
Similarity across many relational databases
59
Bibliography
DB2 cross platform web page includes a pointer to a free DB2
Programming FastPath course download
SQL/400 Developers Guide - Paul Conte and Mike Cravitz
Database Design and Programming for DB2/400 - Paul Conte
S246-0100 DB2/400: The New AS/400 Database: V3R1 - Skip
Marchesani
Informational APARs (II09006)
IBM Publications on the Web
Books
http://www1.ibm.com/servers/eserver/systemi/db2/books.htm
White Papers
http://www1.ibm.com/servers/eserver/systemi/db2/db2awp_m.htm
Information Centre
http://www.systemi.ibm.com/infocentre
GO -> Database and file systems -> DB2 for i5/OS
60
30
Thank You
31