Procedures Implementing Algorithms SQL (Structured Query Language) • SQL Structured Query Language is the standard way of accessing Relational Database • SQL consists of • - DDL (Data Definition Language) • - DML (Data Manipulation Language) • SQL DML statements can be entered interactively SQL … • SQL commands are typed by user when they are to be executed • Results are displayed in a • - GUI (Graphical User Interface) or • - CLI (Command Line Interface) Applications • Often lists of SQL commands must be run many times • It would be difficult to type them in over and over • Users can develop Applications built from Procedures Procedures • A procedure is the implementation of an algorithm. • An algorithm is a list of instructions that perform a specific task • The instructions are executed by a computer from top to bottom • Instructions are executed from top to bottom • The order can be changed by control statements What is an algorithm? Algorithm for driving a car: • Get into the car • Step on the break • If standard, step on clutch • Push “start” button • Put car in gear • Release break • If standard, release clutch Procedures and Functions Procedure - A set of instructions implementing an algorithm. Function - A procedure that returns a value. Procedure format for most languages: • Procedure name (in some languages, not others) • Declarations • Executable instructions Sample Procedure REM Sample PL/SQL program -- This program is kept in a file and run through the command or GUI interface DECLARE x INTEGER; y INTEGER; z INTEGER; BEGIN x := 1001; y := x + 1000; z := (x * 2) + 2000; insert into xtab values (x, y, z); END; / Comments Entered when procedure is written Make a program easy to understand Read by person trying to understand the program In SQL comment lines begin with “REM” or “--” REM Sample PL/SQL program -- This program is kept in a file and run -- through the command or GUI interface Declarations • Many things can be declared for a procedure • Varies from computer language to computer language • Declarations tell the compiler or interpreter what objects will be used in the procedure Things that can be declared Things that can be declared indifferent languages • Variables - named memory location to hold data • New Data Types - Usually based on language supported data types. • Special Variables - example SQL Cursors • Other Procedures and Functions Constants and Variables • A constant is a name given to a value. • A variable is a named memory location that can hold data. • In the quadratic formula AX^2 + BX + C • A, B and C stand for constants • X is a variable that can take on any value How variables are used • Variables can be declared and given values to hold • Declare X Integer; • Tells SQL that X is the name of a memory location that can hold integers. Integers will be assigned to X below. • X := 1001; -- Assigns causes the value 1001 to be stored in X • X := X + 20; -- Will add 20 to what ever is in X and store the sum in X SQL Statements in Procedures • SQL statements can be placed in procedures • They include: Select, Insert, Update, Delete • SQL statements are slightly different in a procedure • Involve variables as well as column names and constants Sample Procedure REM Sample PL/SQL program -- This program is kept in a file and run through the command or GUI interface DECLARE x INTEGER; y INTEGER; z INTEGER; BEGIN x := 1001; y := x + 1000; z := (x * 2) + 2000; insert into xtab values (x, y, z); END; / Assignment Statement • The Assignment Statement assigns a value to a variable DECLARE x INTEGER; BEGIN x := 1001; : -- This is a variable named “X” -- Here the variable “X” is assigned the value 1001 Control Statements • Control Statements alter the flow of instructions in the procedure • They include the “IF-THEN-ELSE” and Looping instructions • Order of instructions depends on a test performed by the control statement. IF-THEN-ELSE • The IF-THEN-ELSE allows conditional execution IF (Department = ‘Sales’) AND (Salary > $20000) THEN INSERT INTO LOG TABLE VALUES (Name, Salary); IF (Department = ‘Sales’) AND (Salary > $20000) THEN INSERT INTO LOG TABLE VALUES (Name, Salary); ELSE UPDATE Stat_Table Set LowVals = LowVals + 1 Where Department = ‘Sales’; End If; Looping Statements • Looping statements allow a sequence of instructions to be repeated X := 0; LOOP X := X + 1; INSERT INTO LogTab Values (101, X); IF X > 20 THEN EXIT; End IF; END LOOP; FOR LOOP • FOR loop can be controlled to loop a given number of times. FOR i in 1..3 LOOP j := i*100; k := j+1; insert into xtab values (i, j, k); END LOOP; While Loop • While loop will loop while a condition is true I := 0; WHILE I < 3 LOOP j := i*100; k := j+1; insert into xtab values (i, j, k); END LOOP; END; Implicit and Explicit Select • SELECT statement can be used inside a procedure • It will take data from the database to be used by the procedure • Implicit SELECT limited to one row. Places data into variables • Explicit SELECT puts many rows in buffer. Pulled into program by a FETCH statement Implicit SELECT statement DECLARE -- Variables to receive the values from the table x INTEGER; y INTEGER; z INTEGER; BEGIN SELECT a, b, c INTO x, y, z -- In this statement a, b, and c are column names FROM xtab WHERE a = 101; : Explicit Select The Explicit SELECT statement requires a cursor You must declare a CURSOR with a SELECT statement CURSOR c1 is SELECT a, b, c from xtab; You must OPEN the CURSOR to run the SELECT statement Rows will be put in a buffer area Open c1 Explicit SELECT continued Use FETCH to access the rows in the buffer one at a time. This is usually done in a loop FETCH c1 INTO x, y, z; Close the CURSOR when you are done CLOSE c1; Explicit Select continued DECLARE CURSOR c1 is SELECT a, b, c from xtab; x INTEGER; y INTEGER; z INTEGER; BEGIN OPEN c1; FOR i in 1..3 LOOP FETCH c1 INTO x, y, z; INSERT into xtab values (x+10, y+10, z+10); END LOOP; CLOSE c1; END; Stored Procedures • Stored Procedures are SQL procedures that are stored in the database • Very similar to PS/SQL procedures • They are created with a DDL statement • They are run with an EXECUTE statement Create Procedure This statement creates a procedure but does not run it Create Procedure ptab (x integer) as begin Update xtab set c=c+100 where a>x; end; Execute Procedure • This statement executes the procedure execute ptab (150); Procedures call procedures Create Procedure ptab2 as begin ptab (150); end; execute ptab2;