Beruflich Dokumente
Kultur Dokumente
for Beginners
Oracle Training
Featuring Sam Weber, OCP Senior DBA, Attask Inc.
What is PL/SQL?
Procedural Language/Structured Query Language is Oracles own language developed to code stored procedures that interacts seamlessly with database objects PL/SQL is similar to other coding languages such as C++ or Java in that it allows the use of declared variables, IF-THEN branching, LOOPS and advanced error handling PL/SQL much more powerful than standard SQL And can be recompiled into native languages.
RESULT DATA
DATA
Oracle Database
RESULT
?
RESULT PL/SQL CALL
Oracle Database
DECLARATION AREA
EXECUTION AREA
V_Title BEGIN
Varchar2(25);
EXCEPTION AREA
DECLARATION section
Cursors, Variables and Arrays declared Optional Doesnt require explicit DECLARE data type can be hard-coded or typed by table_name.column_name
Variables
The name of an address in memory Content may be passed IN or OUT
Assignment done by using:
:=
data type can be hard-coded or typed by table_name.column_name Can be used in equations, functions and predicate comparisons in SQL statements
DECLARE examples
n_Counter Number := 0; v_Type Varchar2(10); v_Boolean Char(1) := T; V_Title MOVIE.TITLE%TYPE;
Exception Handling
Any Oracle error can be mapped to an exception and RAISED Exception block is optional Can print using DBMS_OUTPUT: Special Code Blocks can be executed in the event of specific errors Create your own error messages
What is DBMS_OUTPUT.PUT_LINE?
One of hundreds of PL/SQL packages (stored objects) provided by Oracle in its internal data dictionary Can be called like any other stored object Display hard coded text and values stored in memory to the screen Arguments are passed to the package
Task
Add a 2nd line to your Hello World example displaying the words My name is .
1a
Solution!
SET SERVEROUTPUT ON SIZE 10000 BEGIN DBMS_OUTPUT.PUT_LINE ('HELLO WORLD! My name is Sam'); END; /
BEGIN INSERT INTO MOVIE (TITLE) VALUES ('LORD OF THE RINGS-RETURN OF THE KING'); COMMIT; END; /
ANY DML SQL WILL WORK HERE
Concatenation in PUT_LINE
The double bar ( || ) is used in SQL to blend strings with other strings OR with a column name. PL/SQL adds the ability to display the value of a variable in memory DBMS_OUTPUT.PUT_LINE can use this to display a message:
TASK!
Edit the insert a movie PL/SQL to insert your favorite movie Add a DBMS_OUTPUT.PUT_LINE display to the screen a message showing your favorite movie was added successfully to the movie table
3a
Solution!
DECLARE v_movie varchar2(100); BEGIN v_Movie := 'Avatar'; INSERT INTO MOVIE (TITLE) VALUES (v_movie); COMMIT; DBMS_OUTPUT.PUT_LINE (v_Movie||' inserted successfully'); END; /
4a
Indirect Cursors
DECLARE V_Counter number; BEGIN SELECT COUNT(*) INTO v_Counter FROM MOVIE WHERE upper(TITLE) LIKE 'LORD%'; DBMS_OUTPUT.PUT_LINE ('There are '|| v_Counter || ' movies starting with the word LORD'); END; /
TASK!
Create a PL/SQL program with an indirect cursor which assigns a count of how many movies are of type Comedy Display that number using DBMS_OUTPUT.PUT_LINE
4b
Solution!
DECLARE V_Counter number; BEGIN SELECT COUNT(*) INTO v_Counter FROM MOVIE WHERE upper(type) LIKE 'COMEDY DBMS_OUTPUT.PUT_LINE ('Number of comedy movies: '|| v_Counter ); END; /
Default Values
Default of string values is NULL
An empty string is still NULL in Oracle
Boolean Operators
LOGICAL AND, OR and NOT
IF A < B AND NOT (C = 100
OR
C= 150) THEN D := 5000;
Predicate Operators
=
!= <> > >= <
Equal
Not Equal Greater Than Greater Than or Equal Less Than
<=;
Predicate Operators
BETWEEN
IN LIKE
Arithmetic Expressions
+
* / **
Addition
Subtraction Multiplication Division Power of
()
To group expressions
IF THEN
IF
Statement; ELSIF BOOLEAN expression THEN
BOOLEAN expression
THEN
Counter
V_CNT number := 0
BEGIN LOOP
-----------------------------------V_CNT := V_CNT + 1; END LOOP;
IF THEN Example
IF movie.type = comedy THEN Comedy_count := Comedy_count +1; ELSIF movie.type = action THEN Action_count := Action_count + 1; ELSE Other_count := Other_count + 1; END IF;
IF ! THEN Example
IF NOT movie.type = comedy Other_count := Other_count +1; ELSE Comedy_count := Comedy_count + 1; END IF; ! Can be used instead of NOT THEN
TASK!
Using the PL/SQL program in task 4 Add IF THEN logic to display different messages depending on the number of Comedy Movies found If number is less < 10 then display Few comedy movies elsif > 10 and < 35 then display Some Comedy movies else display Many Comedy Movies; Display the messages using DBMS_OUTPUT.PUT_LINE
5b
Solution!
DECLARE V_Counter number; BEGIN SELECT COUNT(*) INTO v_Counter FROM MOVIE WHERE upper(type) LIKE 'COMEDY'; IF v_Counter < 10 then DBMS_OUTPUT.PUT_LINE ('Few comedy movies'); ELSIF v_Counter BETWEEN 11 AND 35 THEN DBMS_OUTPUT.PUT_LINE ('Some comedy movies'); ELSE DBMS_OUTPUT.PUT_LINE ('Many comedy movies'); END IF; END; /
Simple LOOP
LOOP
Statement; IF BOOLEAN expression THEN
IF
THEN
FOR LOOP
FOR i in a .. b LOOP
Statement; END LOOP;
END;
/
* Executes 11 inserts with seq incrementing by 1 each loop
TASK!
Using the PL/SQL program in lab task 6 Add logic to make the value inserted into the table rows go backwards from 20 to 10 Be aware that the values in your loop need to move forward in sequence, thus you cannot simply switch the values 10 and 20
6b
Solution!
DECLARE v_count number := 20; BEGIN FOR seq in 10 .. 20 LOOP Insert into A (PROC) values (v_count); v_count := v_count - 1; END LOOP; COMMIT; END; /
Direct Cursors
DECLARE
CURSOR c_movie IS
SELECT type FROM movie; BEGIN FOR v_movie in c_movie LOOP -----------------------------------------------------END LOOP; END; /
DECLARE V_count number := 0; BEGIN FOR v_movie in (SELECT type FROM movie) LOOP IF v_movie.type = 'Drama' then v_count := v_count + 1; END IF; END LOOP; DBMS_OUTPUT.PUT_LINE ('Number of Drama movies: ' || v_count); END; /
A standalone script holding a block of PL/SQL code which contains a series of PL/SQL commands designed to be parsed and executed at the same time from an SQL utility. The code is not stored in the Oracle DB as an object.
DECLARE V_count number := 0; BEGIN FOR v_movie in (SELECT TYPE, COUNT(*) CNT FROM movie GROUP BY TYPE) LOOP INSERT INTO MOVIE_COUNT (v_movie.type, v_movie.CNT) END LOOP; DBMS_OUTPUT.PUT_LINE (Movie Counts Inserted . '); END; /
Subprogram Area
Enclosed by parentheses Defines passed parameters both in and out Hard-coded or typed data typing Separated by commas
CREATE OR REPLACE PROCEDURE EMPLOYEE_SALARY_PRC (empno IN Number, deptno IN OUT Varchar2(25), salary OUT EMPLOYEE.SALARY%type)
Procedure
A block of PL/SQL code containing a series of statements which can accept and/or return variables by way of referenced memory.
This is done by looking at or changing their value in memory. If changed, that area of memory can be looked at by the calling block.
Procedure Example
CREATE OR REPLACE procedure movie_TYPE_prc (v_type in varchar2) IS V_count number := 0; BEGIN EXECUTE IMMEDIATE 'TRUNCATE TABLE MOVIE_TEMP'; FOR v_movie in (SELECT TITLE FROM movie WHERE TYPE = v_type) LOOP INSERT INTO MOVIE_TEMP (TITLE) VALUES (v_movie.TITLE); END LOOP; COMMIT; DBMS_OUTPUT.PUT_LINE (v_type || ' movies inserted into temp table'); END; / SHOW ERRORS
9a
Executing a procedure
Function
A block of PL/SQL code containing a series of statements which can accept zero or more variables by memory reference and RETURNING one value to the calling block. An explicit RETURN value statement in the block exits the function and returns the value.
v_Return_Value := Function_Name(passed_in_parameter)
10
Function Example
CREATE OR REPLACE function movie_cnt_fnc (v_Type IN varchar2) RETURN NUMBER IS v_Counter number := 0; BEGIN SELECT COUNT(*) INTO v_Counter FROM MOVIE WHERE TYPE = v_Type; RETURN v_Counter; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE; END; /
10a
Executing a function
DECLARE v_counter number := 0; V_type varchar2(100); BEGIN V_type := 'Horror'; v_Counter := movie_cnt_fnc (v_type); DBMS_OUTPUT.PUT_LINE ('There are ' || v_counter || ' ' || v_type || ' movies'); END; / Function returns a numeric value into the variable
Executing Functions
Executing Functions
From SQL:
SQL> select movie_cnt_fnc (v_type) from dual;
Executing Functions
From a sql PREDICATE Clause
BEGIN IF
END IF;
END;
TASK!
Using the PL/SQL stored proc in lab task 9 Create a new stored procedure which being passed the movie type then calls the function created in lab task 10 and passes the movie type on to it for processing Rerun the procedure passing different types
11a
Solution!
CREATE OR REPLACE procedure movie_TYPE2_prc (v_type in varchar2) IS V_count number := 0; BEGIN v_Count := movie_cnt_fnc (v_type); DBMS_OUTPUT.PUT_LINE ('There are ' || v_count || ' ' || v_type || ' movies'); END; / SHOW ERRORS
Package
A block of PL/SQL code containing a collection of related procedures and/or functions.
There are two parts to a package: A specification lists the available procedures and functions along with their corresponding parameter declarations. The body contains the actual PL/SQL code.
CREATE OR REPLACE package movie_pack AS PROCEDURE movie_count_proc (v_Title IN varchar2); FUNCTION movie_cnt_fnc (v_Title IN varchar2) return number; END; / Specification must be compiled before the body
BEGIN
Moviepack. movie_TYPE2_prc(Horror); END;
Trigger
A block of PL/SQL code containing a series of statements which are attached to an Oracle table. This code is executed whenever a triggering event occurs. This event can be a row inserted, deleted or updated. (individually or any combination) The code can execute once for each row or can be set to execute just once per event. Update triggers can be set to trigger when individual columns change.
Trigger Example
CREATE OR REPLACE TRIGGER CREATE_SCRIPT_NAME_TRG BEFORE INSERT ON DEVTRACKER FOR EACH ROW begin SELECT LPAD(SCRIPT_SEQ.NEXTVAL,5,'0')||'-01DT'||:new.DT_NUM INTO :new.SCRIPT FROM dual; :new.SCRIPT_DATE := SYSDATE; end; /
CREATE OR REPLACE TRIGGER CREATE_SCRIPT_NAME_TRG BEFORE INSERT ON DEVTRACKER FOR EACH ROW begin :new.SCRIPT := LPAD(SCRIPT_SEQ.NEXTVAL,5,'0')||'-01DT'||:new.DT_NUM; :new.SCRIPT_DATE := SYSDATE; end; /
Final Thoughts
PL/SQL is an extraordinary excellent way to transfer code functionality to the database Improve performance time for data transfers Simplified Maintenance Easy to Code Constantly improving
Final Thoughts
Oracle keeps adding new functionality to its packages and more packages Break complicated logic into simpler blocks of called code Automate simple tasks like statistic creation Handy functions make life simpler
Contact Info
sam.weber@attask.com Contact me if you have follow-up questions Check Out www.attask.com on the internet, its a great Project and Portfolio management software for project managers, developers and executives
DBA
Questions?