Upload
hany-paulina
View
80
Download
0
Embed Size (px)
Citation preview
• 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
• 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.
• Interactive SQL
STRSQL
Quick ad-hoc queries
• Embedded SQL
Alternative to native file I/O
Allows for SQL functionality in RPG or COBOL
• 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.
• 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
• 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'
• 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'
• 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
• 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
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
• 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.
• select avg(itmcost) from item_pf
0001.00 010313
0002.00 FItpCost2 O E Printer OflInd(PrtOver) 150612
0003.00 Drecend s n 150614
0004.00 DITEMDS E DS EXTNAME(ITEM_PF:ITEM_FMT) 091212
0005.00 Drata s 5 2 150612
0006.00 020125
0007.00 /Free 020125
0008.00 150614
0009.00 Write Heading; 020125
0010.00 010313
0011.00 EXEC SQL select avg(itmcost) into :rata from item_pf; 150614
0012.00 150614
0013.00 EXEC SQL DECLARE C1 CURSOR FOR SELECT itmnbr, 150614
0014.00 itmdescr, itmcost, itmqtyoh, itmcost * itmqtyoh as 150614
0015.00 OnHand from item_pf where itmcost > :rata; 150614
0016.00 150614
0017.00 EXEC SQL OPEN C1; 091212
0018.00 150614
0019.00 EXEC SQL FETCH C1 INTO :itmnbr, :itmdescr, :itmcost, :itmqtyoh, 150614
0020.00 :itmcostoh; 150614
0021.00 150612
0022.00 DoW not recend; 150327
0023.00 If PrtOver; // Check for overflow 020128
0024.00 Write Heading; 020125
0025.00 PrtOver = *Off; 020125
0026.00 Endif; 020125
0027.00 010313
0028.00 Write Detail; // Print detail record format 150614
0029.00 150612
0030.00 EXEC SQL FETCH C1 INTO :itmnbr, :itmdescr, :itmcost, :itmqtyoh, 150614
0031.00 :itmcostoh; 150614
0032.00 // *** Check whether no more recors to be processed 150614
0033.00 150327
0034.00 recend = (SQLCODE <> 0); 150614
0035.00 150327
0036.00 Enddo; 150614
0037.00 150612
0038.00 EXEC SQL select sum(itmcost * itmqtyoh) into :totcostoh from item_pf 150614
0039.00 where itmcost > :rata; 150614
0040.00 150614
0041.00 EXEC SQL CLOSE C1; 091212
0042.00 Write Total; // Print total record format 030731
0043.00 *InLR = *On; 020125
0044.00 /End-free 020125