Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Introduction to PL/SQL – Lecture 6.

Similar presentations


Presentation on theme: "INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Introduction to PL/SQL – Lecture 6."— Presentation transcript:

1 INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Introduction to PL/SQL – Lecture 6

2 INTRODUCTION TO ORACLE LANGUAGES: PL/SQL PL/SQL is an extension to SQL. Pl/SQL addresses the non-procedural nature of SQL. Programs written in PL/SQL are called subprograms. The three different types of subprograms are: Procedure: A procedure is a program that performs a specific function. Function: Is similar to a procedure but a function has a return clause. Trigger: A trigger is a stored program that is associated with a specific table and is invoked when a specific event occurs. Triggers accept no arguments.

3 INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Program Structure

4 Declare Section Define the type of PL/SQL routine – procedure, function or trigger Define program variables Body Section Between Begin and Exception Clause Main section of the program Exception Section Between the Body Section and the END clause Handles program errors

5 INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Declare Section DEFINING NUMBER VARIABLES counter BINARY_INTEGER; total_cost NUMBER(10,2); seconds_per_day CONSTANT NUMBER := 60 * 60 * 24; final_cost NUMBER(11,0) :=0; DEFINING CHARACTER VARIABLES first_name VARCHAR2(15) NOT NULL := ‘BROWN’; middle_initial VARCHAR2; company_name CONSTANT VARCHAR2(6) := ‘IBM’;

6 INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Declare Section DEFINING DATE VARIABLES hire_date DATE :=‘01-FEB-96’; raise_date DATE; DEFINING BOOLEAN VARIABLES over_weight BOOLEAN NOT NULL :=TRUE absent BOOLEAN := NULL;

7 INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Body Section IF-THEN-ELSE EXAMPLE DECLARE num_jobs NUMBER(8); actor_id NUMBER(4) := 1111; BEGIN SELECT COUNT(*) INTO num_jobs FROM auditions WHERE actorid = actor_id AND called_back = ‘YES’; IF num_jobs > 90 THEN UPDATE actor SET actor_rating = ‘OSCAR winner’ WHERE actorid=actor_id; ELSIF num_jobs > 75 THEN UPDATE actor SET actor_rating = ‘Daytime TV’; ELSE UPDATE actor SET actor_rating = ‘Dish washer’ WHERE actorid = actor_id; END IF; COMMIT; END;

8 INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Body Section LOOP EXAMPLE DECLARE countr number(4) := 0; BEGIN LOOP INSERT INTO my_table VALUES (countr, ‘THE COUNT IS FINISHED’); countr = countr + 1; IF countr = 66 THEN EXIT; END IF; END LOOP; END;

9 INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Body Section CURSOR & LOOP EXAMPLE DECLARE g_total NUMBER(5); sal_top_limit CONSTANT NUMBER(5) :=89000; CURSOR x1 SELECT ename FROM emp WHERE sal > sal_top_limit; BEGIN OPEN x1;. ------- program body. CLOSE x1; END;

10 INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Body Section CURSOR & LOOP EXAMPLE 1 LOOP FETCH the_cursor INTO my_name, my_comm; EXIT WHEN the_cursor%NOTFOUND; ---- more program processing END LOOP; CURSOR & LOOP EXAMPLE 2 FETCH the_cursor INTO my_name, my_sal; WHILE the_cursor%FOUND LOOP ------- more program processing FETCH the_cursor INTO my_name, my_sal; END LOOP;

11 INTRODUCTION TO ORACLE LANGUAGES: PL/SQL – Compiling And Executing SQL > set serveroutput on; - This allows SQL*PLUS to send the PL/SQL output to the screen SQL > @pct_system - This will compile the procedure or function SQL > show errors; - Shows compile errors SQL > execute pct_system; - This will run the procedure. SQL> drop procedure pct_system; - This will drop/remove the procedure from the database

12 INTRODUCTION TO ORACLE LANGUAGES PL/SQL Data Dictionary Views Include: DBA_SOURCE – Stores source code for procedures and functions DBA_ERRORS – Stores compiler error messages. After compiling a program error messages can also be displayed from SQL*PLUS using: SQL > show errors DBA_TRIGGERS - Stores source code, events and other triggers attributes

13 INTRODUCTION TO ORACLE LANGUAGES PL/SQL Functions create or replace function myencryptf( …….PROGRAM VARIABLES………… BEGIN dbms_obfuscation_toolkit.DESEncrypt(input_string => input_s, key_string => key_s, encrypted_string => encrypted_s ); RETURN encrypted_s; EXCEPTION WHEN error_in_input_buffer_length THEN dbms_output.put_line('> ' || INPUT_BUFFER_LENGTH_ERR_MSG); dbms_output.put_line('done '); END; /

14 INTRODUCTION TO ORACLE LANGUAGES Compiling a PL/SQL Function SQL> @c:\scripts\myencryptf Function created. SQL> insert into emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) 2 values 3* (1111,myencryptf('asdfghjk'),'clerk',2222,sysdate,100.00,10.0,10); 1 row created. SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- ----------------- ------ -------- ----------- 1111 n‰?þq clerk 2222 30-JUL-02 1000 10 10

15 INTRODUCTION TO ORACLE LANGUAGES SQL> select ename from emp; ENAME ---------- n‰?þq SQL> @c:\scripts\mydecryptf Function created. SQL> select mydecryptf(ename) from emp; MYDECRYPTF(ENAME) -------------------------------------------------------------------------------- asdfghjk

16 INTRODUCTION TO ORACLE LANGUAGES Triggering Events Include: INSERT/UPDATE/DELETE Types Of Triggers Include: Row Level Trigger - Fires once for each affected row Statement Level Trigger - Fires once for the triggering event (FOR EACH ROW) Triggers must be compiled SQL > @my_trigger - This will compile the trigger.

17 INTRODUCTION TO ORACLE LANGUAGES Row Level Trigger: CREATE OR REPLACE trigger db_trig AFTER INSERT OR UPDATE OR DELETE ON test_table1 DECLARE ….. Statement Level Trigger: CREATE OR REPLACE trigger db_trig AFTER INSERT OR UPDATE OR DELETE ON test_table1 FOR EACH ROW DECLARE ……


Download ppt "INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Introduction to PL/SQL – Lecture 6."

Similar presentations


Ads by Google