3. Note
• Today a Introduction to PL/SQL
• Tomorrow Lab-Session giving hands-on experience
with PL/SQL (Optional)
• Time: September 14, 2002
– 9:00 AM – 11:00 AM
• Location: SB112E
• Please get copies of “Getting Started with PL/SQL”
printed, from CourseInfo Website
6. DECLARE
/* Declarative section: variables, types, and local subprograms. */
BEGIN
/* Executable section: procedural and SQL statements go here. */
/* This is the only section of the block that is required. */
/*Programming Logic – IF, FOR, WHILE*/
EXCEPTION
/* Exception handling section: error handling statements go here. */
END;
PL/SQL Block Structure
7. Illustration
DECLARE
a NUMBER; b NUMBER;
BEGIN
SELECT e,f INTO a,b FROM T1 WHERE e>1;
IF b=1 THEN
INSERT INTO T1 VALUES(b,a);
ELSE
INSERT INTO T1 VALUES(b+10,a+10);
END IF;
END;
8. Procedures
These are the Database Objects. You can reuse them.
CREATE OR REPLACE PROCEDURE name (parameter)
AS
[local declarations]
BEGIN
executable statements;
[EXCEPTION
exception handlers]
END [name];
9. Cursors
• Cursors are memory areas that allow you to
allocate an area of memory and access the
information retrieved from a SQL statement
record by record.
10. Cursors - Illustration
DECLARE
CURSOR cursor_name IS Select….;
var_name cursor_name%ROWTYPE;
BEGIN
Open cursor_name;
Loop
FETCH cursor_name INTO var_name;
EXIT WHEN cursor_name%NOTFOUND;
--Programming Logic
END LOOP;
CLOSE cursor_name;
END;
11. Illustration
CREATE OR REPLACE PROCEDURE Discount
AS
CURSOR c_group_discount
IS
Select * from section;
BEGIN
FOR r_group_discount IN c_group_discount
LOOP
UPDATE course
SET cost = cost * .95
WHERE coutse_no = r_group_discount.course_no;
DBMS_OUTPUT.PUT_LINE
(‘A 5% discount has been given to’ ||
r_group_discount.course_no || ‘ ‘ ||
r_group_discount.description );
END LOOP;
END;
12. Functions
CREATE [OR REPLACE] FUNCTION name
(parameter list)
RETUREN data_type
IS
BEGIN
<body>
RETURN (return_value);
END;
13. Illustrations
CREATE OR REPLACE FUNCTION show_description (i_course_no
number)
RETURN varchar2
AS
v_description varchar2(50);
BEGIN
Select description
INTO v_description FROM course
WHERE course_no = i_course_no;
RETURN v_description;
END;
15. Triggers continued
CREATE [OR REPLACE] TRIGGER trigger_name
{Before/After} trigger_event ON table_name
[FOR EACH ROW]
[WHEN condition]
DECLARE …
BEGIN …
EXCEPTION…
END;
16. Triggers Example
CREATE OR REPLACE TRIGGER student_bi
BEFORE INSERT ON student
FOR EACH ROW
DECLARE
v_student_id STUDENT.STUDENT_ID%TYPE;
BEGIN
SELECT STUDENT_ID_SEQ.NEXTVAL
INTO v_student_id
FROM dual;
:NEW.student_id := v_student_id;
:NEW.created_by := USER;
:NEW.created_date := SYSDATE;
:NEW.modified_date := SYSDATE;
END;
17. References
• Book – “Oracle PL/SQL” – Benjamin
Rosenzweig, Elena Silvestrova
• “Getting Started with PL/SQL” –
– http://ir.iit.edu/~nazli/StartingPLSQL.pdf
19. Example Program
import java.sql.*;
import java.math.*;
import java.io.*;
import java.awt.*;
class Test
{
public static void main ( String args[] ) throws SQLException{
DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver());
Connection conn1
=DriverManager.getConnection("jdbc:oracle:thin:@babbage2.cs.iit.edu:
1521:cs","userid","password");
Statement stmt11 = conn1.createStatement(); // 1st Statement
off conn1
String sql = "SELECT * from test";
ResultSet rset11 = stmt11.executeQuery(sql);
while ( rset11.next() ) {
System.out.println("Result: " +
rset11.getString(1));
rset11.close();stmt11.close();conn1.close();
}
}
}
20. Reminders
• Start using courseinfo.iit.edu for all the
information regarding the course and
discussion board.
• If you have not enrolled onto it yet, make sure
you have yourself enrolled by today. Because
other web pages will not be updated
anymore.
• Assignment 1 – SQL Homework – Due Date:
September 27th no later than 1:40 pm.