Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
1
SQL
Where you want to go Todayfor database access
AgendaWhat is SQLAS/400 database query toolsAS/400 SQL/Query productsSQL ExecutionSQL Statements / ExamplesSubqueriesEmbedded SQL / ExamplesAdvanced SQLSQL Performance / TuningFuture Enhancements to SQLConclusion
2
What is SQLSQL - Structured Query Language
–A standardized language for defining and manipulating data in a relational database.
–English keyword oriented–A tool for the application development environment
QueryData definitionData manipulationData Control
–Set-At-A-Time processing–SQL is not an end user query product
SQL Openness - Standards
SQL in DB2/400 has very high standards compliance
–Entry level of 1992 ANSI, ISO, and FIPS SQL Standard
X/Open SQL Call Level Interface (CLI) provides full compliance with level 1 of Microsoft's ODBC support plus many of level 2 functions.
–Available only in the ILE environment
3
AS/400 database query tools
Open Query File (OPNQRYF) commandQuery/400SQL/400 Query ManagerSQL/400
–Interactive SQL, Statement ProcessorSQL/400 run time support
–SQL Parser, API's, commandsX/Open SQL Call Level InterfaceOS/400 Query Management
Open Query File (OPNQRYF) command
This is a programming command that can be used with a high-level language program. It acts as a filter between the program and the database, so that the program receives only those records that meet the criteria specified in the OPNQRYF command. It is not built on the SQL/400 language.
4
Query/400
This tool allows easy reporting on database information. It is not built on the SQL/400 language.One feature unique to Query/400 is the option of merging data into OfficeVision/400 documents.
SQL/400 Query Manager
This tool allows easy reporting on database information. It is the interface for using the SQL/400 language and OS/400 Query Management. SQL/400 Query Manager provides three integrated interfaces for the creation and maintenance of queries, report forms, and database tables.
5
SQL/400Programmers use the SQL/400 language to query and manipulate data in a database. Most SQL functions can be performed either interactively or in application programs written in a high-level programming language. Interactive SQL can also be used by programmers to test SQL statements.The SQL statement processor allows you to run a series of SQL statements stored in a source file.
SQL/400 run-time support
SQL run-time parses SQL statements and runs any SQL statements. This support is part of the Operating System (OS/400) licensed program. It allows applications that contain SQL statements to be run on systems where the SQL Development Kit is not installed.
6
X/Open SQL Call Level InterfaceAllows any of the ILE languages to access SQL functions directly through procedure calls to a service program provided by the system. Using this interface, one can perform all the SQL functions without the need for a precompile.It is ideally suited for a client-server environment, in which the target database is not known when the application is built.
The Query Management Communications Programming Interface lets users access information on SAA-compliant relational databases and control how this data appears when formatted into a report. This standard is also found on other SAA platforms such as PC's and Mainframes.
OS/400 Query Management CPI
7
DB2/400 Database Manager(Included in OS/400 V3Rx)
Open Query File (OPNQRYF) command–Not built on the SQL/400 language
SQL Parser and run time support–Allows SQL programs to be ported and run on AS/400's not containing SQL kit
–Commands available to create a single runnable SQL statement
SQL API's–QSQPRCED - executes SQL packaged statements–QSQCHKS - checks syntax of SQL statements
X/Open SQL Call Level Interface (V3R2)–ODBC support
Query/400
Separate feature not included in OS/400Same basic interface as the System/36 Query product
8
DB2/400 Query Manager and SQL Development KitSQL/400
– Interactive SQL–RUNSQLSTM command
SQL precompilersThe languages supporting embedded SQL statements:ILE C/400COBOL/400, ILE COBOL/400
FORTRAN/400AS/400 PL/IRPG/400 (III), ILE RPG/400SQL/400 Query ManagerQuery Management
AS/400 SQL ExecutionInteractive SQL
–STRSQLSQL Statement Processor
–RUNSQLSTMQuery Manager
–STRQMWork with queries, report forms, tables, query manager profiles
9
SQL Execution (continued)SQL embedded in HLL programs
–Static SQLSQL statement type and structure known at compile time
–Dynamic SQLSQL statement and structure created at run timeResource intensive
SQL run-time support–SQL Parser, SQL API's–CRTQMQRY, STRQMQRY commands–Default source file - QQMQRYSRC–New object type - QMQRYX/Open SQL CLI
Database Terminology TableSYSTEM TERMS SQL TERMS
AS/400 System Name Database NameLibrary Collection
Physical File TableLogical File View
Record RowField Name Column Name
Data Dictionary CatalogSQL Package Package
10
SQL/400 Statements
Data Definition–CREATE or DROP
Database or CollectionTable (define rows and columns)View (column projection and ordering)Index (row ordering)Procedure (External Procedure)Package (DRDA Remote Support)
–LABEL/COMMENT ON (table/column text)–ALTER TABLE
SQL/400 Statements (cont.)
Data Manipulation–INSERT INTO (rows into tables)–DELETE (rows from tables)–UPDATE (table rows)–SELECT (rows from tables)–OPEN (opens the cursor)–DECLARE CURSOR (set pointer)–FETCH (retrieve columns)–CLOSE (closes the cursor)
11
SQL/400 Statements (cont.)Remote Data Access
–CONNECT–SET CONNECTION–RELEASE–DISCONNECT
Data Access Control–GRANT or REVOKE
Table or PackageData Integrity
–COMMIT–SET TRANSACTION–ROLLBACK–LOCK TABLE
SQL/400 Statements (cont.)
Dynamic SQL–DESCRIBE (retrieves info on PREPARE)–EXECUTE (runs the prepared SQL statement)–EXECUTE IMMEDIATE (prepares and executes an SQL statement)
–PREPARE (create SQL statement from string)
12
SQL/400 Statements (cont.)Miscellaneous
–BEGIN/END DECLARE SECTION (not used in RPG)
–CALL (call external procedure)–DECLARE (Cursor, Procedure, Statement, Variable)
–INCLUDE (insert data set into SQL statements)–SET OPTION (SQL options)–SET RESULT SETS (data from CA/400)–WHENEVER (exception handling)
SELECT (columns, *, or expressions)FROM (tables or views)WHERE (row selection criteria)GROUP BY (columns)
HAVING (GROUP BY selection criteria)ORDER BY (columns)
SELECT*FROM empl
Number Name Position Sex Salary
10 AMY 2 F 120035 JOE 5 M 100030 JON 7 M 150020 DON 5 M 115025 ANN 8 F
SELECT Statement & Clauses
13
SELECT Statement keywords
Keywords in WHERE clause:–Greater than (or =), Less than (or =), Equal–Not greater, Not less, Not equal–AND, OR, NOT–Range - inclusive constant range (BETWEEN, NOT BETWEEN)
–Values - list of constant values (IN, NOT IN)–Pattern matching (LIKE, NOT LIKE) with wild cards
% = any number of characters_ = exactly 1 character
SELECT Where Example
Columns can be reorderedSelection criteria can be applied
SELECT Name, NumberFrom EmplWhere Position = 5
Name Number
JOE 35
DON 20
14
SELECT Where exampleSELECT name, position
FROM EmplWHERE position BETWEEN 5 AND 7
Name Position
JOE 5
JON 7
DON
SELECT name, numberFROM EmplWHERE name LIKE 'A%'
Name Number
AMY 10
ANN
SELECT ORDER BY exampleSELECTname, number, position
FROM EmplWHERE sex = 'M' AND (salary * 12) > 12000ORDER BY name Name Number Position
JOE 35 5
JON 30
SELECTname, number, positionFROM EmplORDER BY 2
Name Number Position
JON 30 7
JOE 35
15
SELECT GROUP BY exampleSELECTposition, SUM(salary), COUNT(*)
FROM EmplGROUP BY positionORDER BY position
Position SUM(Salary) Count
2 1200 1
5 2150 2
7 1500 1
8 1500 1
SELECT WHERE Join exampleNumber Name Position Sex Salary
10 AMY 2 F 120035 JOE 5 M 100030 JON 7 M 150020 DON 5 M 115025 ANN 8 F 1500
Position Description2 Operator 5 Programmer7 Manager8 Analyst
SELECT name, EMPL.position, descriptionFROM Empl, JobWHERE Empl.position = Job.position
Name Position DescriptionAMY 2 OperatorJOE 5 ProgrammerDON 5 ProgrammerJON 7 ManagerANN 8 Analyst
16
Joining TablesWHERE clause used to specify join conditionsJoin columns must have compatible attributes
–number to number, character to character–padding and truncation
Join conditions (=, ~=, <>, <, ~<, <=, >, ~>, >=)SELECT clause can contain:
–columns from any joined table
SQL is a SET-AT-A-TIME languageGive all programmers a 50% salary increase
UPDATE Statement
UPDATE EmplSET salary = salary + (salary * 0.50)WHERE position = 5
17
INSERT StatementOne Row at a time:
INSERT INTO Empl(name, number, position, salary, sex)VALUES ('PAT',11,2,1300,'F')
Multiple Rows at one time:
INSERT INTO EmplSELECT number, name, position, salary, sexFROM EmplnewWHERE position = 9
SQL 'Built-in' FunctionsCan be used anywhere expressions are usedColumn Functions
–Applies to an entire column (grouping of records)Scalar Functions
–Applies to a single value rather than a set of recordsMathmatical functionsLogical ExpressionsString ManipulationDate/Time functionsData format manipulationOther DB/2 functions
18
Column FunctionsAVG
–Average of a numeric columnSUM
–Adds numeric columnMAX
–Maximum value of a numeric or characterMIN
–Minimum value of numeric or characterVARVariance between the sum of 2 numbers COUNT
–Counts the number of rows
Column Functions examples
SELECT COUNT (*)FROM Empl
SELECT MAX(salary), MIN(salary)FROM Empl
COUNT6
MIN MAX1000 1500
19
Scalar FunctionsMathmatical Functions (23)
–COS, SIN, COT, TAN, LOG, SQRT...Logical Expressions (3)
–LAND, LOR, XORString Manipulation(17)
–LOCATE, TRIM, STRIP, SUBSTR, TRANSLATE...Date/Time functions (19)
–DAYOFYEAR, NOW, QUARTER, WEEK...Data format manipulation (11)
–DECIMAL, FLOAT, HEX, INT, ZONED...Other DB/2 Functions (4)
–NODE, PARTITION, RRN
Scalar Function examples
SELECT number, name, YEAR(CURRENT DATE - birthdate)FROM EmplWHERE position = '5'
SELECT *FROM ProjectWHERE SUBSTR(projname,1,10) = 'OPERATION '
Retrieve the number, name, and age of employees in position 5
Select records in project file with the word 'operation' in the project name
20
Sub Queries
A subquery is a SELECT statement embedded in the WHERE or HAVING clause of another SQL query
–also called inner query or nested queryTypes of Subqueries
–returns single value–returns list of values– Inner and Outer queries
Sub Query ExampleSelect maximum salary of employees in position 5
–returns all records having the maximum salary in position 5
SELECT * FROM emplWHERE position = 5and salary =
(SELECT MAX(salary)FROM emplWHERE position = 5)
Number Name Position Sex Salary20 DON 5 M 1150
21
Embedded SQLNew source type
–SQLRPG (RPGIII), SQLRPGLE (RPGIV)SQL uses a precompiler (option 14 in PDM)
–CRTSQLRPG (RPGIII), CRTSQLRPGI (RPGIV)
UserSource
File
ModifiedSource
File
ProcessedSQL
Stmts
Program
AccessPlan
SQLPrecompiler
LanguageCompiler
Syntax CheckingX-ref host variablesSQL statements to callAccess paths to use
Placing SQL in RPGSQL statements executed based on logic in programSQL statements can only be placed in mainline or subroutine of calculation specification
–cannot be placed in total time processingcan call subroutine containing SQL statements
/COPY within RPG code–copy in source with SQL statements
SQL INCLUDE statement within SQL–copy in source with SQL and RPG IV
22
RPG syntax for embedded SQL/EXEC SQL in pos 7 indicates beginning of SQL statement
–SQL statement begins in pos 17 - 80 of this or following lines
/END-EXEC in pos 7 indicates end of SQL statement
–pos 17 - 80 must be blankContinuing the SQL statement
–+ in pos 7, pos 8 must be blank, statement can be in pos 9 - 80
Upper or lower case allowed in SQL codeComment allowed in SQL statement by using an * in pos 7
Host Variables
SQL Host Variable is an HLL variable used in an embedded SQL statementDefining Host variables
–all host variables within an SQL statement must be preceded by a colon (:)
– if the variable exists in more than one table (joined files), the variable name must be prefixed with the table name (or number) and a period
–host variables must be unique within the programdo not use the RPGIV field prefix
23
Host Variable restrictionsAny valid RPG/400 variable name can be used for a host variable except for the following:
–host variable names that begin with the characters 'SQ', 'SQL', 'RDI', 'DSN'. These names are reserved for the database manager.
– indicator field names (*INxx)– tables– look-ahead fields–named Constants–RPG reserved date fields (UDATE, UDAY...)–host variables passed to SQL that have also been used in an RPG CALL/PARM function
if field cannot be used in the result field of the PARM, it cannot be a host variable
Variable restrictions (cont.)
Additional RPG IV restrictions:–multiple dimension arrays–definitions requiring resolution of the field size (*SIZE, *ELEM)
–date formats *JUL, *MDY, *DMY, *YMD can only represent dates from 1940 - 2039.
an error will occur when converting a 4 byte year to these formats when the date is outside of this range.
24
Embedded SQLSummarized column example
Returns total of column for selected rows
C/EXEC SQLC+ SELECT SUM(THRS) INTO :HOURSC+ FROM S#TIMEC+ WHERE CSTCOD = :CSTCOD ANDC+ PROJECT = :PROJECT ANDC+ TNOCH <> 'NC' ANDC+ TTASK <> 'INT'C/END-EXEC
Embedded SQLSelect single row example
I DS 1 50NBRI 6 8 NAMI P 9 132EARNC* C Z-ADD10 NBR C/EXEC SQLC+ SELECT number, name, salaryC+ INTO :nbr, :nam, :earnC+ FROM EmplC+ WHERE number = :nbrC/END-EXEC
NBR NAM EARN10 AMY 1200
25
1. Define RPG fields2. Declare SQL Cursor3. Open Cursor4. Fetch next record5. Process record (UPDATE/INSERT, ect.)6. If last record go to Step 7, else: go to Step 47. Close Cursor
Embedded SQLSELECT multiple rows
Embedded SQLSELECT multiple rows example
DRECDS E DS EXTNAME(EMPL) OCCURS(10):C/EXEC SQLC+ DECLARE C1 FORC+ SELECT *C+ FROM EMPLC/END-EXECC/EXEC SQLC+ OPEN C1C/END-EXEC* top of loopC/EXEC SQLC+ FETCH C1 FOR 10 ROWS C+ INTO :RECDSC/END-EXEC* do something with record(s) and loop :C/EXEC SQLC+ CLOSE C1C/END-EXEC
26
Updates a posted flag to a transaction file after all records in batch are processed
Embedded SQLUpdate example
C EVAL POSTED = 'Y'C EVAL TODAY = UDATEC*C/EXEC SQL UPDATE TRANSC+ SET POSTFLG = :POSTEDC+ WHERE TRNDATE = :TODAYC/END-EXEC
Embedded SQLDynamic SQL statement
C/EXEC SQLC+ PREPARE STMT4SQL FROM :SELECTSTMTC/END-EXECC/EXEC SQLC+ DECLARE NEXTREC CURSOR FOR STMT4SQLC/END-EXECC/EXEC SQLC+ OPEN NEXTRECC/END-EXECC DOU EOF = *ON C/EXEC SQLC+ FETCH NEXTREC INTO :RECDS C/END-EXECC IF SQLCOD <> 0C EVAL EOF = *ONC ELSEC EXSR LOADSFC ENDIFC ENDDOC/EXEC SQLC+ CLOSE NEXTRECC/END-EXEC
Setup data structure RECDS to receive dataPut SQL statements into variable SELECTSTMT
27
Stored ProceduresCalling programs using SQL
PROGRAM: PGMAC/EXEC SQLC+ DECLARE PGMB PROCEDURE** define fields being passed between programs*C+ ( :action IN char(1),C+ :name OUT char(3),C+ :number IN char(5) )** define location of program being called*C+ (External Name JUMPSTART/PGMBC+ Language RPGLEC+ Simple Call)C/END-EXEC:C/EXEC SQLC+ CALL PGMB(:function, :name, :nbr)C/END-EXEC
Program A contains the SQL parameters and statementsProgram B is a normal RPG program (no SQL statements needed)
Stored Procedures (cont.)
PROGRAM: PGMBC *ENTRY PLISTC PARM FUNCTIONC PARM EMPL_NAMEC PARM EMPL_NBR
28
Advanced SQLSQL Communications Area (SQLCA)
–automatically placed into RPG program by SQL precompiler–contains feedback information–SQLCODE or SQLSTATE
SQL error codes (SQLCOD)–0 = statement executed successfully–+n = executed but with warning
+100 'No Data Found'– -n = Error occurred and statement did not run–use SQLCA in HLL or with SQL WHENEVER
SQL Package–normally used to store access plan for remote query
Performance: SQL vs. RPGSQL/400 beats RPG IV in two tests and very close in several other tests:
–SQLinserting large numbers of recordsretrieving records sequentially by key (selected columns)
–RPG IVretrieving records sequentially by key (all columns)updating records sequentiallyretrieving a set of records within a major keyretrieving individual records by primary keyretrieving individual records by RRN
Tests were run under V3R6
29
SQL Performance tips
For high-volume sequential access, tune the block size on the OVRDBF commandUse a blocked insert statement when adding large numbers of new recordsUse a multiple-row Fetch statement when sequentially retrieving 10 or more records at a timeLimit the number of columns you retrieve or update
Tuning SQLPredictive Query Governor
–system predicts and expedites queriesData Management and Query Optimizer
–Data Management algorithms for index usage and row selection–Query Optimizer selects most efficient valid technique for the type of query being processed
Start Database Monitor - STRDBMON (V3R6)Print SQL Information command - PRTSQLINFQuery Debug Messages
STRDBG UPDPROD(*YES)
30
The Predictive Query Governor
Predictive - guesses how long the query will take before it startsIt works with all AS/400 Query products, not just SQL basedCan be used for tuning queriesParameters for system and job
–specify how long of a query can be run–setup the system to either prevent long queries or warn about long queries.
WRKSYSVAL QQRYTIMLMT–sets the value for everyone
CHGQRYA–overrides the query time limit for a job
Print SQL Information command (PRTSQLINF)
Can only be run against a saved access plan–query must have been executed or 'PREPARE' d
information about SQL statements embedded in a program including:
–SQL statements being executed–access plan (access path to be used/created)–estimated run time–parameters used in the SQL precompile
31
CPI4321 - Access path built for fileCPI432F - Access path suggestion for fileCPI4324 - Temporary file built for fileCPI4322 - Access path built for fileCPI4323 - The OS/400 Query access plan has been rebuiltCPI4329 - Arrival sequence access was used for fileCPI432A - Query optimizer timed out for fileCPI432E - Selection fields mapped to different attributesCPI4325 - Temporary result file built for queryCPI4328 - Access path of file was used by query
Query OptimizerTOP 10 Debug Message List
Other SQL Topics
Dynamic SQLCursors
–serial, scrollingUsing Null valuesDate/Time ArithmaticSQL Descriptor Areas (SQLDA)Varying-list SELECT statementIndicator Variables
32
100% compliance with ANSI and ISO SQL standards
–column-level locking (V4R2)–check constraints (V4R2)–default User (V4R2)–SQL-only stored procedures–compound SQL–Case and Cast expressions (V4R2)
DB2 Multisystem support and SMP supportNew and improved X/Open API'sJava Database Connectivity (JBDC)
Future enhancements - V4Rx
SQL ConclusionEnglish keyword oriented language
–easy to learnGreater programmer flexibility
–programming interface into queries allows:run time record selection, file selection, sorting criteriarun time substitution variables
–ad-hoc programming (file conversions)–embedded SQL–ODBC connection
33
SQL/400 Conclusion (cont.)Position for client/server applications
–Java, ODBC/JDBC driver compatibilityMany tools availableCan be optimized and contolled SQL/400 open to other platforms
–SAA-compliant SQL statements are portable to other SAA platforms
–SQL compiled objects can be run without SQL/400 license
Provides table (file) creation function
BibliographyManuals
– SC41-4611 DB2 for OS/400 SQL Programming V3R2– SC41-4612 DB2 for OS/400 SQL Reference V3R2– SC41-3806 DB2 for OS/400 SQL Call Level Interface V3R2
Articles– Dynamic SQL in RPG;Ed Oleson– DB2/400 Query Manager: The New AS/400 Query;Skip Marchesani;Sept/97– Using SQL on the AS/400;Skip Marchesani;Aug/97
34
Bibliography: Perodicals– SQL Minimalism; Thomas Stockwell; Midrange Computing; Apr/96– A quick introduction to SQL with RPG IV; Richard Rubin; News/400; Jan/97– An Introduction to SQL Subqueries; Ted Holt; Midrange Computing; Feb/96– OS/400 Tools to Speed Up SQL and OPNQRYF; Suzan Bestgen & Tom
Schreiber; News/400; Nov/96– SQL/400 vs. RPG IV: Which One's Faster?; Paul Conte; News/400; Sept/96– SQL/400 vs. DDS: Which Should You Use?; Paul Conte; News/400; Sept/96– DB2/400: Coming Attractions; Kent Milligan; News/400; Sept/97