Capitol Tech U Doctoral Presentation - April 2024.pptx
Public Training SQL Implementation & Embedded Programming in IBM i (05-09 Juni 2017)
1.
2. • An alternative database interface language
• NOT a database management system
• High level, simple statement formats
• A language used for:
• Data Definition (DDL)
• Data Manipulation (DML)
• Completely interchangeable data methods
• SQL tables may be accessed with native language
• DDS created files can be access with SQL
3. • Great for selecting and manipulating groups of data
• If you want to update/delete all the records in a file matching a certain
criteria, use SQL. SQL can change or delete a group of records in a single
statement, whereas native I/O would require you to loop through a file
and issue individual update or delete statements.
• Columnar functions allow for column/field manipulation during
the record selection phase
• SQL has many columnar functions designed to tally, total, summarize,
manipulate, and calculate columns of data. Many program features such
as substringing, averaging, and math functions can be performed
during the record selection phase. Columns can even be returned that
don't exist in the file, such as counts, calculations, literals, and dates.
• Aggregate data
• if you wanted to find a list of all the different zip codes in a mailing
address file and count how many addresses were in each zip code, SQL
can easily accomplish this in a single statement. In native I/O you would
have to loop through a file and increment counter fields or use arrays
and/or multiple-occurrence data structures to aggregate like data.
4. • Interactive SQL
STRSQL
Quick ad-hoc queries
• Embedded SQL
Alternative to native file I/O
Allows for SQL functionality in RPG or COBOL
5. • STRSQL (Green Screen)
• System i Navigator
• Most Common SQL Statements
The SELECT statement is used to select data from a database. The result is
stored in a result table, called the result-set.
The UPDATE statement is used to update existing records in a table.
The DELETE statement is used to delete rows in a table.
6. • SELECT *
FROM EMPLOYEE
• SELECT EMPNO, LASTNAME, BIRTHDATE, SALARY
FROM EMPLOYEE
WHERE SALARY > 30000
• SELECT LASTNAME, SALARY, BONUS, COM
FROM EMPLOYEE
WHERE SALARY > 22000 AND BONUS = 400
OR BONUS = 500 AND COM < 1900
ORDER BY LASTNAME
7.
8.
9.
10. • UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
• Be careful when updating records !!!
If you omit the WHERE, all records get
updated
• Example:
UPDATE EMPLOYEE
SET SALARY = SALARY + 1000
WHERE WORKDEPT = 'C01'
11. • DELETE FROM table_name
WHERE some_column=some_value
• BE CAREFUL WHEN DELETING RECORDS!
Like the UPDATE statement, if you omit the
WHERE clause, all records get deleted.
BACK IT UP!
• Example:
DELETE FROM TESTEMP
WHERE EMPNO = '000111'
12. • Static SQL
The simplest form of embedding SQL in RPG or COBOL
The SQL statement is hard coded in your program
• Dynamic SQL
The SQL statement is assembled at run-time
Requires more resource at run-time for preparing the statement
Makes an application very dynamic
Can become very sophisticated
13. • Most variables defined in RPG or COBOL can be used in a SQL
statement
• Variables are preceded by a colon, ( : ).
• Example:
To use RPG or COBOL’s variable field name
ITMNBR in SQL use it as :ITMNBR
14. COST OF ON HAND INVENTORY
ITEM# DESCRIPTION COST QTY OH COST OH
20001 Telephone, one line 15.00 10 150.00
20002 Telephone, two line 89.00 5 445.00
20003 Speaker Telephone 85.00 6 510.00
20004 Telephone Extension Cord 1.10 25 27.50
20005 Dry Erase Marker Packs 2.25 428 963.00
20006 Executive Chairs 325.00 10 3,250.00
20007 Secretarial Chairs 55.00 13 715.00
20008 Desk Calendar Pads 5.00 56 280.00
20009 Diskette Mailers .20 128 25.60
20010 Address Books 6.00 1,680 10,080.00
20011 Desk lamp, brass 20.00 3 60.00
20012 Blue pens 20.00 7 140.00
20013 Red pens 100.00 14 1,400.00
20014 Black pens 150.00 25 3,750.00
20015 Number 2 pencils 2.50 150 375.00
20016 Number 3 pencils 4.50 25 112.50
20017 Two Drawer File Cabinets 5.50 15 82.50
20018 Manilla folders 4.00 50 200.00
20019 Hanging file folders 3.00 150 450.00
20020 Metal desk 125.00 2 250.00
::::: :::::::::::::::::::: :::: ::::: ::::::
20045 Blue paper, 8 1/2 X 11 2.95 75 221.25
20046 Continuous 8,5 X 11 paper 20.00 24 480.00
20047 Yellow paper, 8 1/2 X 11 2.95 199 587.05
20048 3 hole white paper 3.35 35 117.25
20049 3 hole lined paper 3.85 10 38.50
20050 Heavy duty stapler 9.15 5 45.75
TOTAL COST OF INVENTORY ON HAND: 51,755.90
15. • Step 1 – calculate average Item cost
• Read all item records and calculate the ITMCOST average
• Put the average result in AVGCOST
• Step 2 – Select Item cost > Average cost
• Re-Read all item records again and compare the ITMCOST to
AVGCOST
• If the ITMCOST > AVGCOST calculate the amount of
ITMCOST * ITMQTYOH to ITMCOSTOH and print the record,
if NOT read next records
• Print a total of ITMCOSTOH after all records have been
read.