Professional Documents
Culture Documents
Sqlintro Embed PDF
Sqlintro Embed PDF
An Introduction
OCEAN Technical Conference
Catch the Wave
Susan M. Gantner
susan.gantner @ partner400.com
www.partner400.com
Agenda
What is SQL?
SQL language overview
Accessing data using SQL
Creating/maintaining databases using SQL
SQL - 1-2
SQL DDL
DDS definition
IDDU
HLL reads/writes
Query/400
DFU
SQL DML
SQL - 3-4
Terminology
SQL Term
AS/400 Term
Table
File
Row
Record
Column
Field
Table
Nbr
Row
10
35
30
20
25
Name
AMY
JOE
JON
DON
ANN
Pos Sex
2
5
7
5
8
F
M
M
M
F
Sal
1200
1000
1500
1150
1550
Column
Environments
Interactive SQL - Use STRSQL command
Embedded SQL - Put into High Level Language (HLL)
Query Manager - Report Formatter
SQL - 5-6
Name
Pos
Sex
Sal
Nbr
Name
Pos
Sex
Sal
10
AMY
1200
10
AMY
1200
35
JOE
1000
35
JOE
1000
30
JON
1500
30
JON
1500
20
DON
1150
20
DON
1150
25
ANN
1550
25
ANN
1550
Pos
Sex
10
AMY
Name
1200
Sal
35
JOE
1000
30
JON
1500
20
DON
1150
25
ANN
1550
Nbr
Name
35
JOE
20
DON
SQL - 7-8
SELECT statement
Keywords in the WHERE clause:
Greater than (or = ), Less than (or =), Equal
Not greater, Not less, Not equal
AND, OR, NOT
Range - inclusive constant range (BETWEEN)
Values - list of constant values (IN)
Pattern matching (LIKE) with wild cards
% = any number of characters
_ = exactly 1 character
Nbr
Pos
Sex
10
AMY
Name
1200
Sal
Name
35
JOE
1000
JOE
5
7
5
30
JON
1500
JON
20
DON
1150
DON
25
ANN
1550
Pos
SQL - 9-10
Nbr
Name
Pos
Sex
Sal
10
AMY
1200
35
JOE
1000
30
JON
1500
20
DON
1150
25
ANN
1550
Name
Nbr
AMY
10
ANN
25
Nbr
Pos
Sex
10
AMY
Name
1200
Sal
35
JOE
1000
Name
30
JON
1500
20
DON
1150
25
ANN
1550
Nbr
Pos
JON
30
DON
20
SQL - 11-12
Name
Pos
Sex
Sal
10
AMY
1200
35
JOE
1000
Name
30
JON
1500
20
DON
1150
25
ANN
1550
Nbr
Pos
DON
20
JON
30
Pos
Sex
10
AMY
1200
JOE
12000
35
JOE
1000
DON
13800
30
JON
1500
AMY
14400
20
DON
1150
25
ANN
1550
JON
ANN
18000
18600
Name
Sal
Name
Sal
SQL - 13-14
Name
Pos
Sex
Sal
Pos AVG(Sal)
10
AMY
1200
1200
35
JOE
1000
1075
30
JON
1500
1500
20
DON
1150
1550
25
ANN
1550
Nbr
Pos
Sex
10
AMY
1200
35
JOE
1000
30
JON
1500
20
DON
1150
25
ANN
1550
Name
Sal
Pos AVG(Sal)
7
1500
1550
SQL - 15-16
JOB Table
Pos
Sex
Desc
10
AMY
1200
Operator
35
JOE
1000
Programmer
30
JON
1500
Manager
20
DON
1150
Analyst
25
ANN
1550
Pos
Nbr
Name
Name
Pos
Sal
Desc
AMY
Operator
JOE
Programmer
JON
Manager
DON
Programmer
ANN
Analyst
SQL - 17-18
UPDATE Statement
Give all programmers (pos = 5) a 10% raise!
UPDATE empl
SET sal = sal + (sal * .10)
WHERE pos = 5
Nbr
Name
Pos
Sex
Sal
Pos
Sex
10
AMY
1200
Nbr
10
AMY
Name
1200
Sal
35
JOE
1000
35
JOE
1100
30
JON
1500
30
JON
1500
20
DON
1150
20
DON
1265
25
ANN
1550
25
ANN
1550
INSERT Statement
Add new rows using INSERT
Column names and values in one-to-one correspondence
One row at a time using the VALUES clause:
SQL - 19-20
DELETE Statement
Rows can be deleted individually or by sets as well
DELETE
FROM empl
WHERE nbr = 10
Nbr
Name
Pos
Sex
Sal
Pos
Sex
10
AMY
1200
Nbr
35
JOE
Name
1100
Sal
35
JOE
1000
30
JON
1500
30
JON
1500
20
DON
1265
20
DON
1150
25
ANN
1550
25
ANN
1550
SQL - 21-22
Creating Tables
Tables are created as physical files
Can be accessed same as any other PF
With or without SQL
SQL Views
Contain a selection of columns and/or rows from base table
May be a subset of columns and/or rows
May be a join view
SQL - 23-24
SQL Indexes
Creates a keyed logical file over table(s)
Used primarily to enhance performance
Note: ALL keyed logical files may be used to improve performance
Even if not created as SQL indexes
OS/400 contains:
Run-time support for SQL and Query Manager
QM & SDK not required to run SQL applications or pre-created QM
queries
SQL - 25-26
Interactive SQL
A tool for programmers and database administrators
Interactive functions
Quickly maintain database
Test SQL code before embedding
Create test data scenarios
STRSQL to begin
Interactive SQL
F4=Prompt
_______________
_______________
_______________
_______________
_______________
_______________
_______________
F5=Refresh
F12=Cancel
SQL - 27-28
Embedded SQL
Why embed SQL in programs?
Perform dynamic selection functions
ala OPNQRYF, except more flexible
User
Source
File
Precompile
Modified
Source
File
Processed
SQL Stmts
Compile
Program
Access
Plans
(temporary)
SQL - 29-30
*
D
D
D
No F spec needed !
EmpNbr
Name
Job
S
S
S
5 0
25
1
C/EXEC SQL
C+
SELECT NAME, POS
C+
INTO :Name, :Job
C+
FROM EMPL
C+
WHERE NBR = :EmpNbr
C/END-EXEC
SQL - 31-32
S9(5) COMP-3.
S9(3) COMP-3.
X(25).
PROCEDURE DIVISION.
EXEC SQL
SELECT name, pos
INTO :nam, :job
FROM empl
WHERE nbr = :EmpNbr
END-EXEC.
D EMP
D Job
D Name
D Sal
DS
EmpNbr
5
25
7
0
2
C/EXEC SQL
C+ SELECT
POS, NAME, SAL
C+
INTO :EMP
C+
FROM
EMPL WHERE NBR = :EmpNbr
C/END-EXEC
SQL - 33-34
ELSE go to Step 3
6. Close cursor
C/EXEC SQL
C+
DECLARE empcsr CURSOR FOR
C+
SELECT nbr, nam, sal
C+
FROM emp
C+
WHERE dpt = :dept
C+
C/END-EXEC
SQL - 35-36
Considerations:
FOR READ ONLY - may improve performance; better documentation
FOR UPDATE OF - security; may improve performance
C/EXEC SQL
C+
DECLARE empcsr CURSOR FOR
C+
SELECT nbr, nam, sal
C+
FROM emp
C+
WHERE dpt = :dept
C+
FOR UPDATE OF sal
C/END-EXEC
C/EXEC SQL
C+
C+
DECLARE empcsr CURSOR FOR
C+
WITH HOLD
C+
SELECT nbr, nam, sal
C+
FROM emp
C+
WHERE dpt = :dept
C+
FOR UPDATE OF sal
C+
C/END-EXEC
SQL - 37-38
OPEN statement
Actually executes the SQL Select statement
Builds the access path if necessary
Successful Open places the file cursor before the first row of
the result table
Cursor must be closed before it can be opened
Syntax: OPEN cursor-name
C/EXEC SQL
C+
C+
OPEN
C+
C/END-EXEC
empcsr
FETCH statement:
Two functions
position the cursor for the next operation
C/EXEC SQL
C+
C+
FETCH
C+
C/END-EXEC
NEXT
FROM
empcsr
SQL - 39-40
FETCH statement
Alternatives to Next processing:
must define the cursor as a scrollable cursor in the declare statement
C/EXEC SQL
C+
C+
DECLARE empcsr SCROLL CURSOR
C+
SELECT nbr, nam, sal
C+
FROM emp
C+
ORDER BY empid
C+
C/END-EXEC
FOR
C/EXEC SQL
C+
C+
FETCH PRIOR FROM empcsr
C+
INTO :number, :name, :salary
C+
C/END-EXEC
FETCH statement
Keyword
Next
Prior
First
Last
Before
After
Current
Relative n
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)
n < -1 Positions to nth row before current
n = -1 Same as Prior keyword
n = 0 Same as Current keyword
n = 1 Same as Next keyword
n > 1 Positions to nth row after current
SQL - 41-42
Close Statement
Close the cursor
Cursor must be opened in order to be closed
C/EXEC SQL
C+
C+
CLOSE
C+
C/END-EXEC
empcsr
SQL - 43-44
/EXEC
SQL
INCLUDE SQLCA
/END-EXEC
Char(8)
Integer
Integer
SmallInt
Char(70)
Char(8)
Array of Integers
SQL - 45-46
Char(11)
Char(1)
SQLWarn1
SQLWarn2
SQLWarn3
SQLWarn4
SQLWarn5
SQLWarn6
SQLWarn7
SQLWarn8
SQLWarn9
SQLWarnA
SQLState
Char(1)
Char(1)
Char(1)
Char(1)
Char(1)
Char(1)
Char(1)
Char(1)
Char(1)
Char(1)
Char(5)
SQL - 47-48
RPG
SQLCod < 0
Error
SQLCod = 100
NotFound
EXEC SQL
SELECT name INTO :lastname
WHERE emp = Employee-Number
END-EXEC.
IF SQLCODE < 0
PERFORM ERROR-ROUTINE.
IF SQLCODE = 100
PERFORM NOT-FOUND-ROUTINE.
COBOL
Three conditions:
SQLWARNING (SQLCODE > 0 except 100)
OR (SQLWARN0 = 'W')
SQL - 49-50
SQL - 51-52
C
C
Eval
C
C
C
C
C
If
Eval
Else
Eval
EndIf
DeleteCorp
Condition = 'Corp = ?'
C
C
Eval
C/EXEC SQL
C+
C+
PREPARE
FROM
C/END-EXEC
C
C/EXEC SQL
C+
C+
EXECUTE
C/END-EXEC
C
DynSQLStmt
:SQLStmt
If
DynSQLStmt
Using :Cust
EndIf
SQL - 53-54
Compile commands
Pre-ILE compilers
CRTSQLRPG, CRTSQLCBL
ILE compilers
CRTSQLRPGI, CRTSQLCBLI
Creates either *PGM, *SRVPGM or *MODULE depending on parameter
value specified for "Compile type" or OBJTYPE
SQL - 55-56
Put your job in debug mode before running SQL program, then look in
your joblog
Additional messages are put in joblog when in debug mode which can be
helpful in diagnosing SQL performance problems
Performance Tips
SQL uses two basic ways to retrieve data
Dataspace scan or arrival sequence
Generally used when MORE than 20% of records will be selected
Create indexes for fields that are frequently used to join files
Use PRTSQLINF command and/or job log debug messages to
see if indexes are being used by SQL optimizer
SQL - 57-58
Query Manager
With Query Manager users can:
Create, run and manage queries and report forms
Create, manage and query database files
QM Table support allows creation and data entry facilities
Summary
Practical, effective solution for applications requiring complex
data retrieval
Very flexible, functional application development tool
embedded in a HLL program
or entered interactively
SQL - 59-60