+ All Categories
Home > Documents > Embedded SQL RPG

Embedded SQL RPG

Date post: 07-Jul-2016
Category:
Upload: moises-von-dixon
View: 279 times
Download: 4 times
Share this document with a friend
Description:
RPG SQL
31
1 Embedding SQL in High Level Language Programs Alison Butterill IBM i Product Manager Power Systems © 2012 IBM Corporation 2 Introduction Basic SQL within a HLL program Processing multiple records Error detection Dynamic SQL Tips and Techniques Summary Agenda
Transcript
Page 1: Embedded SQL RPG

1

Embedding SQL in High Level Language Programs

Alison ButterillIBM i Product Manager

Power Systems

© 2012 IBM Corporation2

IntroductionBasic SQL within a HLL programProcessing multiple recordsError detectionDynamic SQLTips and TechniquesSummary

Agenda

Page 2: Embedded SQL RPG

2

© 2012 IBM Corporation3

What is SQL?

SQL - Structured Query Language

Data language for manipulation of a Relational database

English keyword-oriented

Excellent tool for application development environment

Query Data definition Data manipulation Data control

Powerful language for Set-At-A-Time processing

SQL is NOT an end user query product!

© 2012 IBM Corporation4

Executing SQL on IBM i

Interactive SQL Embedded or compiled in application programsQuery Manager SQL Statement Processor Operations NavigatorDynamic SQL SQL Procedure LanguageExtended Dynamic SQL ODBC, JDBCJSQL or SQLJX/Open SQL Call Level Interface

Page 3: Embedded SQL RPG

3

© 2012 IBM Corporation5

V5 SQL Support...

Part 1: DB2 Database Manager Included with IBM i (operating system)

V5: 5722-SS1 SQL parser and run time support

SQL license (5722-ST1) not required to run SQL applications Support for compiled programs using embedded SQL

SQL APIs QSQPRCED - Provides extended dynamic SQL capability QSQCHKS - Provides syntax checking for SQL statements

X/Open SQL Call Level Interface V5R1 - SQL Statement Processor

RUNSQLSTM Command

© 2012 IBM Corporation6

V5 SQL Support

Part 2: DB2 for IBM i Query Manager & SQL Development Kit

Program number 5722-ST1

Query Manager

Interactive SQL

SQL precompilers

Required for embedding SQL in HLL programsMore later

SQL REXX interface

Page 4: Embedded SQL RPG

4

© 2012 IBM Corporation7

IntroductionBasic SQL within a HLL programProcessing multiple recordsError detectionDynamic SQLTips and TechniquesSummary

Agenda

© 2012 IBM Corporation8

RPG

COBOL

C

Java

AS/400 PL/I

FORTRAN/400

High Level Languages Supported

Page 5: Embedded SQL RPG

5

© 2012 IBM Corporation9

User

Source

File

Modified

Source

File

Processed

SQL

Statements

Program

Access

Plan

SQLPrecompiler

LanguageCompiler

Syntax checkX-ref host variablesSQL statements to callsComment SQL statements

The SQL Precompiler

© 2012 IBM Corporation10

Basic commandsSELECT - retrieves data; one row or multipleUPDATE - updates one row or multipleDELETE - deletes one row or multipleINSERT - adds one row or multiple

Additional functions for complex processingDECLARE CURSOR – builds temp result tableOPEN and CLOSE – open/close result tableFETCH – row retrievalCOMMIT and ROLLBACK – journaling functionsGRANT and REVOKE – security functions

SQL Commands

Page 6: Embedded SQL RPG

6

© 2012 IBM Corporation11

Basic RPG Interface

•Retrieve column/field values into program variables•One-to-one correspondence between SELECT list and INTO list

•SELECT … INTO expects only a single row/record–Multiple rows require the use of cursor operations

I DS I 1 50 EMPNBR I 6 30 NAM I 31 32 DEPT C* C/EXEC SQL C+ SELECT nbr, nam, dpt C+ INTO :empnbr, :nam, :dept C+ FROM emp C+ WHERE nbr = :empnbr C/END-EXEC

© 2012 IBM Corporation12

Basic Cobol Interface

WORKING-STORAGE SECTION. 77 EMPNBR PIC S9(5) COMP-3. 77 DEPT PIC S9(3) COMP-3. 77 NAM PIC X(25).

. PROCEDURE DIVISION.

EXEC SQL SELECT nbr, nam, dpt

INTO :empnbr, :nam, :dept FROM empWHERE nbr = :empnbr

END-EXEC.

•Retrieve column/field values into program variables•One-to-one correspondence between SELECT list and INTO list

•SELECT … INTO expects only a single row/record–Multiple rows require the use of cursor operations

Page 7: Embedded SQL RPG

7

© 2012 IBM Corporation13

Prompting for SQL

Use SQL source member types–e.g., SQLRPG, SQLRPGLE, SQLCBL, SQLCBLLE–Prompting will not work without SQL member type

SEU supports prompting today; RSE does not todayBoth EXEC SQL and END-EXEC statements must be in place

–Then prompting (F4) will work for statements in between–Same prompter as interactive SQL (Start ISQL command)

Compile commands–OPM compilers

CRTSQLRPG, CRTSQLCBL–ILE compilers

CRTSQLRPGI, CRTSQLCBLICreates either *PGM, *SRVPGM or *MODULE depending on parameter value specified for "Compile type" or OBJTYPE

© 2012 IBM Corporation14

Any SQL statement can be prompted from within a source program

Prompting Embedded SQL

Columns . . . : 1 71 Edit SKIP/QRPGSRC SEU==> _____________________________________________ AASQLTST FMT ** ...+... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+.

*************** Beginning of data ********************** 0011.00 C/EXEC SQL 0012.00 ==>> C+ <<=== Prompt from 0013.00 ==>> C+ SELECT <<=== any of 0014.00 ==>> C+ <<=== these lines 0015.00 C/END-EXEC

****************** End of data *************************

F3=Exit F4=Prompt F5=Refresh F9=Retrieve F10=Cursor F16=Repeat find F17=Repeat change F24=More keys

Page 8: Embedded SQL RPG

8

© 2012 IBM Corporation15

Specify SELECT Statement

Type information for SELECT statement. Press F4 for a list.

FROM files . . . . . . . emp_______________________________SELECT fields . . . . . nbr,_nam,_sal_____________________WHERE conditions . . . . dpt_=_:dept_______________________GROUP BY fields . . . . __________________________________HAVING conditions . . . __________________________________ORDER BY fields . . . . __________________________________FOR UPDATE OF fields . . sal_______________________________

Bottom Type choices, press Enter.

Number of records to optimize . . . . . . __________ DISTINCT records in result file . . . . . N Y=Yes, N=NoFOR FETCH ONLY . . . . . . . . . . . . . . N Y=Yes, N=NoUNION with another SELECT . . . . . . . . N Y=Yes, N=No

F3=Exit F4=Prompt F5=Refresh F6=Insert line F9=SubqueryF10=Copy F12=Cancel F14=Delete F15=Split line F24=More keys

Prompting for Embedded SQL

© 2012 IBM Corporation16

Columns . . . : 1 71 Edit SKIP/QRPGSRC SEU==> _____________________________________________ AASQLTST FMT ** ...+... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+.

*************** Beginning of data ********************** 0011.00 C/EXEC SQL 0012.00 C+ 0013.00 C+ SELECT nbr, nam, sal 0013.01 C+ FROM emp 0013.02 C+ WHERE dpt = :dept 0013.03 C+ FOR UPDATE OF sal 0014.00 C+ 0015.00 C/END-EXEC

****************** End of data *************************

F3=Exit F4=Prompt F5=Refresh F9=Retrieve F10=Cursor F16=Repeat find F17=Repeat change F24=More keys

Prompting for Embedded SQL…

Results of Prompting:

Page 9: Embedded SQL RPG

9

© 2012 IBM Corporation17

Embedded SQL in Free Format RPG

Start of SQL statement

SQL Statement

© 2012 IBM Corporation18

Using Structures in SQL

Host structures are groups of variables

–Data structures in RPG

–Group items in COBOL

Structures can be used in SQL statements

Replaces list of variables

Page 10: Embedded SQL RPG

10

© 2012 IBM Corporation19

I empds DS I 1 50 EMPNBR I 6 30 NAM I 31 31 DEPT C* C/EXEC SQL C+ SELECT nbr, nam, dpt C+ INTO :empds C+ FROM emp C+ WHERE nbr = :empnbr C/END-EXEC

Using HLL Structures with SQL

WORKING STORAGE SECTION.01 EMP_DATA.

05 NBR PIC S9(5) COMP-3.05 DEPT PIC S9(1) COMP-3.05 NAME PIC X(25).

PROCEDURE DIVISIONMOVE 5 TO NBR.EXEC SQL

SELECT nbr, pos, namINTO :EMP_DATAFROM empl WHERE pos=:DEPT

END-EXEC.

RPG

COBOL

© 2012 IBM Corporation20

IntroductionBasic SQL within a HLL programProcessing multiple recordsError detectionDynamic SQLTips and TechniquesSummary

Agenda

Page 11: Embedded SQL RPG

11

© 2012 IBM Corporation21

Steps to access multiple rows:

1 Declare SQL cursor

2 Open cursor

3 Fetch next record

4 Process record (UPDATE/INSERT/etc)

5 If last record: go to Step 6,

else: go to Step 3 6 Close cursor

Selecting and Processing Multiple Rows

© 2012 IBM Corporation22

Selecting and Processing Multiple Rows …

DECLARE CURSOR Statement•Similar in function to HLL file declarations

–F-specs or FD's–No processing actually takes place - just definition

•Host variables may be included in the statement•Created using an embedded SELECT command

–most SELECT clauses may be used - ORDER BY, GROUP BY, etc•Coded in C-specs of RPG or Procedure Division of COBOL•Must be declared before being referenced

C* C/EXEC SQL C+ C+ DECLARE empcsr CURSOR FOR C+ SELECT nbr, nam, salC+ FROM empC+ WHERE dpt = :dept C+ C/END-EXEC C*

Page 12: Embedded SQL RPG

12

© 2012 IBM Corporation23

By default, all columns may be updated or deleted–FOR UPDATE OF - lists the columns that are to be updated

only columns NOT included in ORDER BY are eligible–FOR READ ONLY - specifies no updating/deleting allowed

Considerations:–FOR READ ONLY - may improve performance; better documentation

–FOR UPDATE OF - security; may improve performance –FOR READ ONLY with “insensitive” clause

Selecting and Processing Multiple Rows …

DECLARE CURSOR Statement – additional clausesC* C/EXEC SQL C+ C+ DECLARE empcsr CURSOR FOR C+ SELECT nbr, nam, salC+ FROM empC+ WHERE dpt = :dept C+ FOR UPDATE OF salC+ C/END-EXEC C*

© 2012 IBM Corporation24

With Hold clause useful with commitment controlDefault – cursors are closed with commit/rollback commandsWith Hold – keeps cursor openWith Hold also an optional clause on the commit/rollback statements

Selecting and Processing Multiple Rows …

DECLARE CURSOR Statement – additional clauses

C* C/EXEC SQL C+ C+ DECLARE empcsr CURSOR FOR C+ WITH HOLDC+ SELECT nbr, nam, salC+ FROM empC+ WHERE dpt = :dept C+ FOR UPDATE OF salC+ C/END-EXEC C*

Page 13: Embedded SQL RPG

13

© 2012 IBM Corporation25

OPEN Statement

•Executes the SELECT Statement For a Declared Cursor

•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

C*C/EXEC SQL C+ C+ OPEN empcsr C+ C/END-EXEC C*

© 2012 IBM Corporation26

FETCH Statement

C* C/EXEC SQL C+ C+ FETCH NEXT FROM empcsr C+ INTO :number, :name, :salary C+ C/END-EXEC

Two Functions

Position the cursor for the next operation

Bring rows/records into the program

C*C/EXEC SQL C+ C+ FETCH NEXT FROM empcsr C+ C/END-EXEC

Page 14: Embedded SQL RPG

14

© 2012 IBM Corporation27

Selecting and Processing Multiple Rows …

Keyword Positions CursorNext On the next row after the current rowPrior On the row before the current rowFirst On the first rowLast On the last row Before Before the first row - must not use INTOAfter After the last row - must not use INTOCurrent On the current row (no change in position)

Relative n n < -1 Positions to nth row before currentn = -1 Same as Prior keywordn = 0 Same as Current keywordn = 1 Same as Next keywordn > 1 Positions to nth row after current

FETCH StatementAlternatives to Next processingCursor must be defined as a scrollable

© 2012 IBM Corporation28

Selecting and Processing Multiple Rows …

C/EXEC SQL C+ C+ DECLARE empcsr SCROLL CURSOR FOR C+ SELECT nbr, nam, sal C+ FROM emp C+ ORDER BY empidC+ C/END-EXEC C* C/EXEC SQL C+ C+ FETCH PRIOR FROM empcsr C+ INTO :number, :name, :salary C+ C/END-EXEC

FETCH StatementAlternatives to Next ProcessingCursor must be defined as a scrollable

Page 15: Embedded SQL RPG

15

© 2012 IBM Corporation29

Updates or deletes the current row of an updatable cursorCan only be done after successful Fetch operation"Where Current of" clause in Update and Delete statements

C/EXEC SQL C+ DECLARE empcsr CURSOR FOR C+ SELECT nbr, nam, sal C+ FROM emp C+ ORDER BY empid C+ FOR UPDATE OF salC/END-EXEC C* C/EXEC SQL C+ FETCH NEXT FROM empcsr C+ INTO :number, :name, :salary C/END-EXEC C* C/EXEC SQL C+ UPDATE emp C+ SET sal = sal + :raise C+ WHERE CURRENT OF empcsrC/END-EXEC

Selecting and Processing Multiple Rows …

Positioned Update and Delete Statements

© 2012 IBM Corporation30

•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

CLOSE Statement

C* C/EXEC SQL C+ C+ CLOSE empcsr C+ C/END-EXEC

Selecting and Processing Multiple Rows:

Page 16: Embedded SQL RPG

16

© 2012 IBM Corporation31

Selecting and Processing Multiple Rows …

C* C/EXEC SQL C+ C+ DECLARE empcsr CURSOR FOR C+ SELECT nbr, nam, sal C+ FROM emp C+ WHERE dpt = :dept C+ FOR UPDATE OF sal C+ C/END-EXEC C* C EXFMT PROMPT C*C/EXEC SQLC+C+ OPEN empcsrC+C/END-EXEC

Example: Update SALARY for all records in a specified department

Load

Define

NOTE: Not all program logic is shown!!

© 2012 IBM Corporation32

C* C/EXEC SQL C+ C+ FETCH NEXT FROM empcsr C+ INTO :number, :name, :salary C+ C/END-EXEC C* C/EXEC SQL C+ C+ UPDATE empC+ SET sal = sal + :raise C+ WHERE CURRENT OF empcsrC+ C/END-EXEC C* C/EXEC SQL C+ C+ CLOSE empcsrC+ C/END-EXEC

Selecting and Processing Multiple Rows …

Example Continued

Process

Close

Retrieve

NOTE: Not all program logic is shown!!

Page 17: Embedded SQL RPG

17

© 2012 IBM Corporation33

IntroductionBasic SQL within a HLL programProcessing multiple recordsError detectionDynamic SQLTips and TechniquesSummary

Agenda

© 2012 IBM Corporation34

•Status always returned to HLL code –both successful and unsuccessful statements

•Programmer must check return codes within program•SQL Communications Area (SQLCA)

–contains feedback information–must be included in all SQL programs–RPG includes SQLCA automatically–other languages must have specific include:

EXEC SQL

INCLUDE SQLCA

END-EXEC

Error Detection and Handling

Page 18: Embedded SQL RPG

18

© 2012 IBM Corporation35

Error Detection and Handling

SQLCAID Char(8) Structure identifying literal: "SQLCA"SQLCABC Integer Length of SQLCASQLCode Integer Return codeSQLErrML SmallInt Length of SQLErrMCSQLErrMC Char(70) Message Replacement textSQLErrP Char(8) Product ID literal: "QSQ" for DB2/400SQLErrD Array of Integers SQLErrD(1) - treated as Char(4); last 4 characters

of CPF or other escape message SQLErrD(2) - treated as Char(4); last 4 characters

of CPF or other diagnostic message SQLErrD(3) - for Fetch, Insert, Update or Delete,

number of rows retrieved or updated SQLErrD(4) - for Prepare, relative number

indicating resources required for execution

SQLErrD(5) - for multiple-row Fetch, contains 100 if last available row is fetched; for Delete, number of rows affected by referential constraints; for Connect or Set Connection, contains t-1 if unconnected, 0 if local and 1 if connection is remote

SQLErrD(6) - when SQLCode is 0, containsSQL completion message id

SQL Communications Area (SQLCA)

© 2012 IBM Corporation36

Error Detection and Handling

SQL Communications Area (SQLCA) - continued

SQLWarn Char(11) Set of 11 warning indicators; each is blank, W, or NSQLWarn0 Char(1) Blank if all other SQLWARNx warning indicators are blank

W if any warning indicator contains W or NSQLWarn1 Char(1) W if a string column was truncated when assigned to host variable

SQLWarn2 Char(1) W if null values were eliminated from a functionSQLWarn3 Char(1) W if number of columns is larger than number of host variables

SQLWarn4 Char(1) W if prepared Update or Delete statement doesn't include a Whereclause

SQLWarn5 Char(1) ReservedSQLWarn6 Char(1) W if date arithmetic results in end-of-month adjustmentSQLWarn7 Char(1) ReservedSQLWarn8 Char(1) W if result of character conversion contains the substitution

characterSQLWarn9 Char(1) ReservedSQLWarnA Char(1) ReservedSQLState Char(5) Return code; "00000' if no error or warning

Page 19: Embedded SQL RPG

19

© 2012 IBM Corporation37

Error Detection and Handling

•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.

SQLCODE Values

© 2012 IBM Corporation38

Error Detection and Handling

C/EXEC SQLC+ SELECT name INTO :namC+ WHERE emp = 100C/END-EXECC SQLCOD IFLT 0 C EXSR ERRC ENDIFC SQLCOD IFGT 0C EXSR NFC ENDIF

EXEC SQLSELECT name INTO :lastname

WHERE emp = 100END-EXEC.IF SQLCODE < 0

PERFORM ERROR-ROUTINE.ELSEIF SQLCODE > 0

PERFORM NOT-FOUND-ROUTINE ELSE

PERFORM GOOD-REC-ROUTINE.

COBOL

RPG

Page 20: Embedded SQL RPG

20

© 2012 IBM Corporation39

Error Handling and Detection

C* C/EXEC SQL C+ C+ WHENEVER SQLERROR GO TO error C+ C/END-EXEC

•WHENEVER statement checks SQLCA•Can branch to a location based on condition•Three conditions:

–SQLWARNING (SQLCODE > 0 except 100)OR (SQLWARN0 = 'W')

–SQLERROR (SQLCODE < 0)–NOT FOUND (SQLCODE = 100)

•Two possible actions–CONTINUE–GO TO label

WHENEVER Statement

© 2012 IBM Corporation40

Error Detection and Handling – Better Way

•Directly following each SQL statement - code an 'error catcher'•Easier to determine source of the error•More discrete method of determining action to be taken

C* C/EXEC SQL C+ C+ DECLARE empcsr CURSOR FOR C+ SELECT nbr, nam, sal C+ FROM emp C+ WHERE dpt = :dept C+ FOR UPDATE OF sal C+ C/END-EXEC C* C EXFMT PROMPT C* Read operation of EXFMT retrieve dept number

Example: Update SALARY for all records in a specified department

NOTE: Not all program logic is shown!!

Define

Page 21: Embedded SQL RPG

21

© 2012 IBM Corporation41

Selecting and Processing Multiple Rows …

C*C/EXEC SQLC+ OPEN empcsrC/END-EXECC* C IF SQLCODE <> 0C EXSR ErrC DOU = 100C*

C/EXEC SQL C+ FETCH NEXT FROM empcsrC+ INTO :number, :name, :salary C/END-EXEC C* C IF SQLCODE <> 0C EXSR ErrC DOU = 100 C*

Example Continued

NOTE: Not all program logic is shown!!

Load

Retrieve

© 2012 IBM Corporation42

C* C/EXEC SQL C+ UPDATE empC+ SET sal = sal + :raise C+ WHERE CURRENT OF empcsrC/END-EXEC C* C IF SQLCODE <> 0C EXSR ErrC ENDIF C ENDIFC ENDDOC* C/EXEC SQL C+ CLOSE empcsrC/END-EXEC

Selecting and Processing Multiple Rows …

Example Continued

NOTE: Not all program logic is shown!!

Process

Close

Page 22: Embedded SQL RPG

22

© 2012 IBM Corporation43

IntroductionBasic SQL within a HLL programProcessing multiple recordsError detectionDynamic SQLTips and TechniquesSummary

Agenda

© 2012 IBM Corporation44

•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

Dynamic SQL

What is Dynamic SQL?

Page 23: Embedded SQL RPG

23

© 2012 IBM Corporation45

•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

Dynamic SQL

What is Dynamic SQL?

© 2012 IBM Corporation46

Where to use Dynamic SQL

Report programs with user run time selection

–Files–Fields–Record selection criteria–Sorting–SQL built in functions

Whenever the exact syntax of an SQL statement cannot be determined beforehand

Dynamic SQL

Page 24: Embedded SQL RPG

24

© 2012 IBM Corporation47

Dynamic SQL – Example 1

C*C Eval SQLStmtStr = 'Delete From CustomerC Where 'C Eval SQLStmt = SQLStmtStr + InpCondC/EXEC SQLC+ C+ PREPARE DynSQLStmtC+ FROM :SQLStmtC+C/END-EXEC C*C If (SQLCod = 0) And (SQLWn0 = *Blank) C/EXEC SQLC+ C+ EXECUTE DynSQLStmtC+C/END-EXEC C*

User prompted to enter the delete condition (InpCond variable)

© 2012 IBM Corporation48

Dynamic SQL

C* User prompted to enter delete condition (InpCnd)C* C/EXEC SQL C+ C+ PREPARE search FROM :sqlstmC+ 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

Example 2: Dynamically select records from the Employee Master File - Any fields, records, or sequence

Page 25: Embedded SQL RPG

25

© 2012 IBM Corporation49

Performance considerations

Dynamic SQL can be resource intensive

Remember that a dynamic SQL statement is parsed

(interpreted) and executed within the application program

from which it is called

–May have negative impact on performance

Use it ... But use it carefully!

Dynamic SQL

© 2012 IBM Corporation50

IntroductionBasic SQL within a HLL programProcessing multiple recordsError detectionDynamic SQLTips and TechniquesSummary

Agenda

Page 26: Embedded SQL RPG

26

© 2012 IBM Corporation51

Performance Tips

•Test statements in Interactive SQL before embedding them–When exiting Interactive SQL session

You can save session statements to a source memberThen 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

Embedded SQL Tips

© 2012 IBM Corporation52

•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

Performance Tips

Designing and Writing Efficient SQL Queries

Page 27: Embedded SQL RPG

27

© 2012 IBM Corporation53

•PRTSQLINF–Prints information about the embedded SQL statements in a program, SQL package or service program

SQL statementsAccess plansList of command parameters used by pre-

compiler•STRDBMON •Predictive Query Governor

–CHGQRYA command•TRCJOB •Visual Explain (first in V4R5)

–Operations Navigator

Performance Tips

Performance Analysis Tools

© 2012 IBM Corporation54

Performance Tips

•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

Multiple Row FETCH

D EMP DS Occurs(10)D NBR 5 0D NAME 25D JOB 1C*C Z-ADD 5 JOBC/EXEC SQLC+ FETCH NextC+ FROM CustomerCursorC+ FOR 10 ROWSC+ INTO EmpC/END-EXECC Eval ErrCond = SQLErrD(5) C*

Page 28: Embedded SQL RPG

28

© 2012 IBM Corporation55

Performance Tips

Multiple Row FETCH - continued

First row fetched- placed into first element of host structureFetching stops when n rows are returned - or no more recordsProgram variable may be used to specify number of records to be fetched (Note: RPG IV - %Elem built in function)Be 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 ROWSResults:

–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

© 2012 IBM Corporation56

Performance Tips

Blocked INSERT

D EMP DS Occurs(10)D NBR 5 0D NAME 25D JOB 1C*C/ EXEC SQLC+ INSERT INTO CustomerC+ 10 ROWSC+ VALUES :EmpC/ END-EXEC

•Multiple elements from a host structure are inserted into a table

•Program variable may be used to set the number of records to be inserted

•Executes as if n INSERT statements had been issued–but with improved performance

Page 29: Embedded SQL RPG

29

© 2012 IBM Corporation57

Other Uses for Embedded SQL

•Remote Database Access–CONNECT TO–DISCONNECT–RELEASE ALL–SET CONNECTION

•Stored Procedures–DECLARE PROCEDURE–CALL PROCEDURE

•INCLUDE statement•Dynamic Cursor•Query Manager

© 2012 IBM Corporation58

IntroductionBasic SQL within a HLL programProcessing multiple recordsError detectionDynamic SQLTips and TechniquesSummary

Agenda

Page 30: Embedded SQL RPG

30

© 2012 IBM Corporation59

•Practical, effective solution for applications

requiring complex data retrieval

•Very flexible, functional tool for development

–Embedded in a HLL program

–Entered interactively

•Portability to other relational databases

–Easy report writing with programmable

flexibility

•Similarity across many relational databases

Summary

© 2012 IBM Corporation60

Bibliography

•DB2 cross platform web page – includes a pointer to a free DB2 Programming FastPath course download•SQL/400 Developer’s 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://www-1.ibm.com/servers/eserver/systemi/db2/books.htm

•White Papershttp://www-1.ibm.com/servers/eserver/systemi/db2/db2awp_m.htm

•Information Centrehttp://www.systemi.ibm.com/infocentreGO -> Database and file systems -> DB2 for i5/OS

Page 31: Embedded SQL RPG

31

Thank You


Recommended