[go: up one dir, main page]

Academia.eduAcademia.edu
PL/ SQL: Introduction Exceptions Cursors Procedures Functions Packages Triggers    Introduction:- 1) What is Pl/SQL ? It is extension to SQL language. PL/SQL = SQL + Programming features. The following are the advantages of PL/SQL 1) We can use programming features like If stmt, loops, branching etc; 2) We can have user definied error messages by using the concept of exception handling. 3) We can perform related actions by using the concept of triggers. 4) Pl/SQL helps in reducing the network traffic. PL/SQL Block structure: ---------------------- declare ........... -- Declare section ........... ........... begin ........... -- Executable section .......... ........... ........... ........... exception .......... -- Exception section .......... end; / A Pl/SQL block contains 3 sections. 1) Declare section 2) Executable section 3) Exception Section 1) Declare section: ------------------- It is used to declare local variables, Cursor , exceptions etc;. All the lines between declare and begin is called declare section.This section is optional. 2) Executable Section: ---------------------------- The actual task which should be done is written in the executable section. All the lines between Begin and exception keywords is called as Executable section. This section is mandatory 3) Exception Section: ----------------------- If an exception is raised in the executable section, control enters into exception section. All the lines between exception and end is called exception section. This section is optional. Ex1: ---- Write a PL/SQL block to display 'Hello World'. For this program, we do not need any local variables. So, we can start the program by using keyword begin. Before the running this program, we need to make the environment variable serveroutput to ON. To command to make the serveroutput to ON] SQL> Set serveroutput on Begin dbms_output.put_line('Hello World'); end; / Hello World Pl/SQL procedure successfully completed. Ex 2: ------- Write a PL/SQL block to calculate sum of two numbers. For this program, we need 3 variables, so we need declare section. Syntax to declare variable: ---------------------------- <variable> <datatype>(size); Declare a number(3); b number(3); c number(4); begin a :=10; b :=20; c := a+b; dbms_output.put_line ( ' The sum is ...'||c); end; / The sum is ...30 Pl/SQL procedure successfully completed. In the above program, there are two important points to learn. i) := is assignment operator, which is used to assign value from the right hand side to the variable in the left hand side. ii) || (pipe) is concatenation operator. We can initilize at the time of declaration. declare a number(3) :=10; b number(3) :=20; In the abvoe program, we have hard coded the value 10 and 20 in the program. Instead of hard coding the value, we can accept the values from the user. Ex 3: --- Write a program to accept two values from the user and display its sum. Declare a number(3); b number(3); c number(4); begin a := &a; b := &b; c := a+b; dbms_output.put_line('The sum is ...'||c); end; / Enter a value for A:40 Enter a value for B:30 The sum is ...70 Pl/SQL procedure successfully completed. Note: & operator is used to accept value from the user. Ex 4: ------ Write a PL/SQL block to accept empno and increments his salary by 1000. Note: To increment the salary (change the value) in a table, we need to use update command. Declare l_empno number(4); begin l_empno := &empno; update emp set sal = sal+1000 where empno = l_empno; end; / Enter a value for empno: 7900 Procedure successfully completed. To make the above update command permanent, we can use commit after update command in PL/SQL block. ex: --- Declare l_empno number(4); begin l_empno := &empno; update emp set sal = sal+1000 where empno = l_empno; commit; end; / Writing a select stmt in a PL/SQL Block: ------------------------------------------- Write a pl/SQL block which accepts empno and display ename and salary. As ename and sal are the values present in the emp table, to get those values we need to write a select stmt. Note: Every select stmt in a PL/SQL block should have into clause. Declare l_empno number(4); l_ename varchar2(20); l_sal number(5); begin l_empno := &empno; select ename,sal into l_ename, l_sal from emp where empno = l_empno; dbms_output.put_line(l_ename||'....'||l_sal); end; / Note: -------- As the above select stmt selects two columns, we need two local variable to catch the value returned by the select stmt. Using %TYPE attribute: ------------------------ %TYPE attribute is used to declare the local variables. Instead of hardcoding the datatype and size for local variable, we can use %TYPE attribute. Ex: l_ename varchar2(20); -- we are hard coding datatype and size l_ename emp.ename%TYPE; --- The datatype of ename column of emp table is applicable to the local variable. The above program, i use %TYPE attribute to declare local variables. Declare l_empno emp.empno%TYPE; l_ename emp.ename%TYPE; l_sal emp.sal%TYPE; begin l_empno := &empno; select ename,sal into l_ename, l_sal from emp where empno = l_empno; dbms_output.put_line(l_ename||'....'||l_sal); end; / Using %ROWTYPE Attribute: -------------------------- A ROWTYPE variable is capable of holding complete row of table. Ex: ------ Write a PL/SQL Block which accepts an empno and display ename, sal, hiredate and job. declare l_empno emp.empno%TYPE; l_row emp%ROWTYPE; begin l_empno := &empno; select * into l_row from emp where empno = l_empno; dbms_output.put_line(l_row.ename); dbms_output.put_line(l_row.sal); dbms_output.put_line(l_row.hiredate); dbms_output.put_line(l_row.job); end; / Note: we cannot print a ROWTYPE variable, we can print a value of a ROWTYPE variable.  Exceptions:- 1) What is Exception? Every error in Oracle is an exception. 2) Types of exceptions? Exceptions are divided into three types 1) Pre definied exceptions 2) NoN pre definied exceptions 3) User definied exceptions Pre Definied Exceptions: ----------------------------- These exceptions will have exception name and exception number. The following are some of the examples of predefinied exceptions. EXCEPTION_NAME EXCEPTION_NUMBER 1) NO_DATA_FOUND 2) TOO_MANY_ROWS 3) ZERO_DIVIDE 4) VALUE_ERROR 5) DUP_VAL_ON_INDEX 1) NO_DATA_FOUND : ------------------------ This exception is raised when select does not return any row in PL/SQL block. ex: --- declare l_sal emp.sal%type; begin dbms_output.put_line('Welcome'); select sal into l_Sal from emp where empno = 2255; dbms_output.put_line('The sal is ....'||l_sal); dbms_output.put_line('Thank You'); end; / Output: --------- Welcome error Note: In the above program, we get the output 'Welcome'. This means that program execution is started. As we dont have any employee with empno 2255, select stmt does not return any row. When select stmt does not return any row, NO_DATA_FOUND exception is raised. Once an exception is raised, control will not execute the remaining stmts of executable section, searches for Exception section. As we do not have exception section in the program, it is terminated abnormally. We can make sure that the program is completed normally by catching the exception using Exception section. Syntax: ---------- Declare ......... ......... begin ........ ........ ......... Exception When <Exception_handler> then .................... .................... end; / Ex: --- declare l_sal emp.sal%type; begin dbms_output.put_line('Welcome'); select sal into l_Sal from emp where empno = 2255; dbms_output.put_line('The sal is ....'||l_sal); dbms_output.put_line('Thank You'); Exception when NO_DATA_FOUND then dbms_output.put_line('Invalid empno'); end; / Output: ------ Welcome Invalid empno Pl/SQL Procedure successfully completed. 2) TOO_MANY_ROWS: ---------------------- TOO_MANY_ROWS exception is raised, when select stmt returns more than one row. Ex: ---- declare l_sal emp.sal%type; begin dbms_output.put_line('Welcome'); select sal into l_Sal from emp where deptno=10; dbms_output.put_line('The sal is ....'||l_sal); dbms_output.put_line('Thank You'); end; / Output: ----------- Welcome Error Note: --------- As we get the output 'Welcome', this means that program execution is started. As the select stmt returns more than one row, TOO_MANY_ROWS exception is raised. As we know, Once an exception is raised control will not execute the remaining lines of excutable section, searches for the Exception section. As we do not have exception section, program is terminated abnormally. We can avoid abnormal termination of the program by catching the Exception. Ex: --- declare l_sal emp.sal%type; begin dbms_output.put_line('Welcome'); select sal into l_Sal from emp where deptno=10; dbms_output.put_line('The sal is ....'||l_sal); dbms_output.put_line('Thank You'); Exception When TOO_MANY_ROWS then dbms_output.put_line( 'Select stmt returns more than one row'); end; / Output: -------- Welcome Select stmt returns more than one row. Pl/SQL Procedure successfully completed. 3) ZERO_DIVIDE: ----------------- This exception is raised, when we divide a number by zero. Ex: ---- Declare a number(4); begin dbms_output.put_line('Welcome'); a := 10/0; dbms_output.put_line(a); dbms_output.put_line('Thank You'); end; / Output: -------- Welcome Error Note: ------ In the above program, as we are dividing by zero, ZERO_DIVIDE exception is raised. As we are not catching the exception, program is terminated abnormally. As a developer, we need to make sure that programs are completed successfully at any case. SO we need to handle exception which is raised by using the Exception Section. Ex: -------- Declare a number(4); begin dbms_output.put_line('Welcome'); a := 10/0; dbms_output.put_line(a); dbms_output.put_line('Thank You'); Exception When ZERO_DIVIDE then dbms_output.put_line('DO not divide by 0'); end; / Output: ------- Welcome DO not divide by 0. Pl/SQL Procedure successfully completed. 4) VALUE_ERROR: ----------------------- This exception is raised, when the value which is returned does not match with the datatype variable. Ex: ------- Declare l_ename number(10); begin dbms_output.put_line('Welcome'); select ename into l_ename from emp where empno = 7369; dbms_output.put_line('The employee name is...'||l_ename); end; / Output: ------- Welcome Error Note: -------- As the select stmt returning char value, it cannot be stored in varible of number data. In this case VALUE_ERROR exception is raised. As we are not catching the exception, program is terminated abnormally. We can avoid abnormal termination of the program by catching the exception using Exception Section. Ex: ---- Declare l_ename number(10); begin dbms_output.put_line('Welcome'); select ename into l_ename from emp where empno = 7369; dbms_output.put_line('The employee name is...'||l_ename); Exception when VALUE_ERROR then dbms_output.put_line('Pl check the datatype of the local variables'); end; / Output: -------- Welcome Pl check the datatype of the local variables 5) DUP_VAL_ON_INDEX: ---------------------------------------- This exception is raised when we try to insert a dulicate value on a primary key or unique key. ex: -------- Create the following table: create table student ( sno number(3) primary key, sname varchar2(20), marks number(3)); insert a row in the table: insert into student values (101,'arun',40); commit; begin dbms_output.put_line ( 'Welcome'); insert into student values (101,'vijay',50); dbms_output.put_line ( 'Thank You'); end; / Output: ----------- Welcome Error Note: --------- As we are inserting a duplicate value in a primary key column, DUP_VAL_ON_INDEX exception is raised. As we are not catching the exception program is terminated abnormally. We can avoid abnormai termination of the program by catching the exception. Ex: ------- begin dbms_output.put_line ( 'Welcome'); insert into student values (101,'vijay',50); dbms_output.put_line ( 'Thank You'); Exception when DUP_VAL_ON_INDEX then dbms_output.put_line('Do not insert duplicate value in a primary key'); end; / Output: ------------- Welcome Do not insert duplicate value in a primary key When Others handler: --------------------------------- When others can handle any type of exception Ex1: -------- Declare a number(4); begin dbms_output.put_line('Welcome'); a := 10/0; dbms_output.put_line(a); dbms_output.put_line('Thank You'); Exception When others then dbms_output.put_line('Pl check the code'); end; / Output: ----------- Welcome Pl check the code Note: Exception that is raised is ZERO_DIVIDE. We do not have ZERO_DIVIDE handler, but When Others can handler can handle this exception. Ex2: --------- declare l_sal emp.sal%type; begin dbms_output.put_line('Welcome'); select sal into l_Sal from emp where deptno=10; dbms_output.put_line('The sal is ....'||l_sal); dbms_output.put_line('Thank You'); Exception When others then dbms_output.put_line('Pl check the code'); end; / Output: ------------- Welcome Pl check the code +++++++++++++++++++++++++++++++++++ Non predefinied exception: ------------------------------------- These exceptions will have exceptio number , but does not have exception name. Ex: ------- ORA-2292 exception. This exception is raised when we try to delete from row from the parent table if correspoding row exists in the child table. First lets establish parent-child relationship between two tables. create table student2( sno number(3) primary key, sname varchar2(20), marks number(3)); insert into student2 values (101, 'arun',40); insert into student2 values (102, 'varun',50); insert into student2 values (103, 'kiran',60); create table library2 ( roll_no number(3) references student2(sno), book_name varchar2(20)); insert into library2 values (101,'Java'); insert into library2 values (102,'C++'); insert into library2 values (102,'Oracle'); commit; begin dbms_output.put_line('Welcome'); delete from student2 where sno =101; dbms_output.put_line('Thank You'); end; / Output: ----------- Welcome Error Note: We are deleteting the row from the parent table and the corresponding row exists in the child table. So exception is raised. The exception which is raised in the above program is ORA-2292. This exception does not have any name. This is an example of non -predefinied exception. The following steps are to followed to handle non-pre definied exception. Step 1: Declare the exception Step 2: Associate the exception Step 3: Handle then exception. Syntax: ----------- Step 1: Declare the exception <Exception_name> Exception; Step 2: Associate the exception raise_application_error ( <exception_no> , <Exception_name> ); Step 3: Handle the exception Exception When < Exceptionb_name> then ............ ........... ............ end; / Ex: --- In the follwoing program , we perform the three step process to handle Non-pre definied exceptions. Declare MY_EX1 Exception; Raise_application_error ( -2292 , MY_EX1 ); begin dbms_output.put_line('Welcome'); delete from student2 where sno =101; dbms_output.put_line('Thank You'); Exception When MY_EX1 then dbms_output.put_line('Cannot delete from the parent table'); end; / Output: ------------ Welcome Cannot delete from the parent table 3) User definied exceptions: -------------------------------- These exceptions are definied by the user. Following steps are to be followed to handle user definied exceptions. Step 1: Declare the exception Step 2: Raise the exception Step 3: Handle the exception. Ex: --- Declare l_sal emp.sal%type; my_ex1 exception; begin dbms_output.put_line('Welcome'); select sal into l_sal from emp where empno =7902; if l_sal > 2000 then raise my_ex1; end if; dbms_output.put_line('The sal is ....'||l_sal); Exception When my_ex1 then dbms_output.put_line(' Sal is too high'); When others then dbms_output.put_line('Pl check the code'); end; / Output: ------------ Welcome Sal is too high Using raise_application_error: ------------------------------------------ raise_application_error is a procedure which is used to throw a user defined error error_number and error_message to the application. Ex: ------ Declare l_sal emp.sal%type; begin dbns_output.put_line('Welcome'); select sal into l_sal from emp where empno = 7902; if l_sal > 2000 then raise_application_error ( -20150, ' Sal is too high'); end if; dbms_output.put_line('The sal is ....'||l_sal); end; / Ouptut: ---------- Welcome ORA-20150 , Sal is too high Error Reporting functions: ------------------------------------ Cursors:- Cursor is a memory locations which is used to run SQL commands. There are two types cursors 1) Implicit Cursors 2) Explicit Cursors 1) Implicit Cursors: ---------------------------- All the activited related to cursor like i) Opening the cursor ii) Processing the data in the cursor iii) closing the cursor are done automatically. Hence these cursors are called Implict cursors. Implicit Cursor Attributes: --------------------------------------- There are four Implicit cursor attributes 1) SQL%ISOPEN 2) SQL%FOUND 3) SQL%NOTFOUND 4) SQL%ROWCOUNT 1) SQL%ISOPEN: -------------------------- It is a boolean attribute. It always returns false. It is not used in programming as it always returns false. 2) SQL%FOUND: ----------------------------- It is a boolean attribute. Returns TRUE -- if the SQL command effects the data. Returns FALSE -- if the SQL commands do not effect the data. 3) SQL%NOTFOUND: -------------------------------- It is a boolean attribute Returns TRUE -- if the SQL command do not effect the data. Returns FALSE -- if the SQL command effects the data Note: It is exactly negation to SQL%FOUND 4) SQL%ROWCOUNT: ------------------------------- Returns no of rows effected by the SQL command. Using SQL%FOUND: ----------------------------- Begin Update emp set sal=2000 where empno=1111; end; / Output: ------------- PL/SQL Procedure successfully completed. By looking at the above message, we cannot know whether your update command is effecting the data or not. To overcome this problem, we have SQL%FOUND attribute. Have a look at this program Begin Update emp set sal=2000 where empno=1111; if SQL%FOUND then dbms_output.put_line('Update is successfull'); else dbms_output.put_line('Update is failed'); end if; end; / Output: ------------ Update is failed. PL/SQL Procedure successfully completed. Using SQL%NOTFOUND: ------------------------------ SQL%NOTFOUND is exactly opposite to SQL%FOUND. We rewrite the above program using SQL%NOTFOUND Begin Update emp set sal=2000 where empno=1111; if SQL%NOTFOUND then dbms_output.put_line('Update is failed'); else dbms_output.put_line('Update is successful'); end if; end; / Output: ------------ Update is failed. PL/SQL Procedure successfully completed. Using SQL%ROWCOUNT: ----------------------------------- SQL%ROWCOUNT attribute is used to find the no of rows effected by SQL command. begin update emp set sal=2000 where deptno=10; dbms_output.put_line(SQL%ROWCOUNT||' rows updated'); end; / Output: ----------- 3 rows updated. Note: As a developer, we cannot control the implicit cursor. We can you these implicit cursor attributes to know whether the command is effecting the data or not. Explicit Cursors: ----------------------- Explicit cursors are used to run select stmt which returs more than one row in a PL/SQL block Steps to use Explicit cursors: ------------------------------------ Step 1: Declare the cursor Step 2: Open the cursor Srep 3: Fetch the data from the cursor to the local variables Step 4: close the cursor Syntax of the above four steps: ----------------------------------------------- Step 1: Declaring the cursor cursor < cursor_name> is < select stmt >; step 2: Open the cursor open < cursor_name >; step 3: Fetch the data from the cursor to the local variables fetch < cursor_name > into < var1 > , < var2> , ....., < varn >;; step 4: close the cursor close < cursor_name>; Explicit cursor attributes: ---------------------------------- There are four explicit cursor attributes 1) %ISOPEN 2) %FOUND 3) %NOTFOUND 4) %ROWCOUNT 1) %ISOPEN: -------------------- It is a boolean attribute. Returns TRUE -- if the cursor is open Returns FALSE -- if the cursor is closed 2) %FOUND: ------------------ It is a boolean attribute Returns TRUE -- if the fetch stmt is successfull Returns FALSE -- if the fetch stmt fails 3) %NOTFOUND: -------------------------- It is boolean attribute Returns TRUE -- if the fetch stmt fails. Returns FALSE -- if the fetch stmt is successfull Note: 1) It is exactly opposite to %FOUND attribute 2) This attribute is used to break the loop of the fetch stmt. 4) %ROWCOUNT: --------------------------- Returns no of rows fetched by the fetch stmt. Example of Explicit cursor: ----------------------------------- Write a PL/SQL block to display ename and sal of employees working in deptno no Declare cursor c1 is select ename , sal from emp where deptno=10; l_ename emp.ename%type; l_sal emp.sal%type; begin open c1; loop fetch c1 into l_ename , l_sal; exit when c1%notfound; dbms_output.put_line( l_ename||'....'||l_sal); end loop; close c1; end; / Output: ------------ CLARK 2450 KING 5000 MILLER 1300 Pl/SQL Proceudure successfully completed. Ex2: Write a PL/SQL procedure to display dname , loc from dept table Declare cursor c1 is select dname , loc from dept; l_dname dept.dname%type; l_loc dept.loc%type; begin open c1; loop fetch c1 into l_dname, l_loc; exit when c1%notfound; dbms_output.put_line(l_dname||'.....'||l_loc); end loop; close c1; end; / Output: -------------- Accounting New York Research Dallas Sales Chicago Operations Boston Pl/SQL Procedure successfully completed. Cursor For loops: ------------------------ It is shortcut way of writing explicit cursors. When we use cursor for loops , following steps are not required. 1) Open the cursor 2) Fetch stmt 3) exit when condition 4) closing the cursor 5) declaring the local variables Ex: ---------- Write a PL/SQL block which display ename and sal of employees working in deptno 10 Declare cursor c1 is select ename , sal from emp where deptno=10; begin for emp_rec in c1 loop dbms_output.put_line(emp_rec.ename||'.....'||emp_rec.sal); end loop; end; / Output: -------------- CLARK 2450 KING 5000 MILLER 1300 Pl/SQL Proceudure successfully completed. Note: In the above program emp_rec in implicitly declared record variable, which is capable of storing one row of the cursor. Procedures:- A Procedure is a named PL/SQL block which is compiled and stored in the database for repeated execution. Basic Syntax : ------------ Create or replace procedure <procedure_name> is begin .............. .............. ............. end; / Ex 1: ----------- Create or replace procedure p1 is begin dbms_output.put_line('Hello World'); end; / Procedure created. To execute the procedure: ---------------------------- Exec command is used to execute the procedure. SQL> Exec p1 Hello World A procedure can have three types of parameters. 1) IN Parameter 2) OUT Parameter 3) IN OUT Parameter In Parameters are used to accept values from the user. Ex 2: --------- Create a procedure which accepts two numbers and display its sum. create or replace procedure add_num ( a IN number, b IN number) is c number(3); begin c := a+b; dbms_output.put_line(' The sum is '||c); end; / Procedure created. To execute the procedure: -------------------------- SQL> exec add_num (10,20) Ex 3: -------- Create a Procedure which accepts an empno and increments his salary by 1000. create or replace procedure inc_sal ( a in number) is begin update emp set sal = sal+1000 where empno = a; end; / Procedure created. TO execute the procedure: --------------------------- SQL> exec inc_sal(7900) We can improve the above procedure code by using %type attribute in procedure parameters. The above procedure can be re-written as below : create or replace procedure inc_sal ( a in emp.empno%type) is begin update emp set sal = sal+1000 where empno = a; end; / Ex 4: ------- Create a procedure which accepts empno and display ename and salary. create or replace procedure display_emp ( l_empno emp.empno%type) is l_ename emp.ename%type; l_sal emp.sal%type; begin select ename, sal into l_ename,l_sal from emp where empno = l_empno; dbms_output.put_line(l_ename||'....'||l_sal); exception when no_data_found then dbms_output.put_line('Invalid empno'); end; / Ex 5: -------- Create a procedure which accepts deptno and display ename and salary of employees working in that department. create or replace procedure display_emp1 (l_deptno emp.deptno%type) is cursor c1 is select ename,sal from emp where deptno = l_deptno; begin for emp_rec in c1 loop dbms_output.put_line(emp_rec.ename||'...'||emp_rec.sal); end loop; end; Ex 6: -------- We can call a procedure from another procedure. create or replace procedure demo1 is begin dbms_output.put_line('This is from demo1'); end; / create or replace procedure demo2 is begin dbms_output.put_line ('Welcome'); demo1; dbms_output.put_line ('Thank you'); end; / SQL> Exec demo2 Ex 7: --------- We can call multiple procedures at a time using PL/SQL block. begin p1; add_num(10,20); inc_sal(7900); end; / Ex 8: -------- If there are any syntax errors in the procedure code, then the procedcure is created with compilation errors. create or replace procedure add_num ( a IN number, b IN number) is c number(3); begin c := a+b; dbms_outut.put_line(' The sum is '||c); end; / Procedure is created with compilation errrors. To see the errors, use the following command. SQL> sho err We get error information. Rectify the error and re compile the code to create procedure successfully. Ex 9: --------- Sub procedure: A procedure inside another procedure is called as Sub procedure. create or replace procedure test is procedure sample is begin dbms_output.put_line('This is from sample'); end; begin dbms_output.put_line('This is from test'); sample; end; In the above example procedure sample is called as Sub procedure. A Sub procedure can be invoked from the main procedure only. SQL> EXEC test This is from test This is from sample We cannot invoke the Sub procedure independently. The following command will give error. SQL>EXEC sample Ex 10: ---------- OUT parameters are used to return the values to the calling environment. create a procedure which accepts empno and return salary. create or replace procedure ret_sal( l_empno in emp.empno%type, l_sal out emp.sal%type) is begin select sal into l_sal from emp where empno = l_empno; end; As the procedure is returning a value using OUT parameter, we need to have a bind variable to catch the value. We need to follow a 3 step process to execute the above procedure. Step 1: Create bind variable Step 2: Execute the procedure using bind variable Step 3: Print the value in the bind variable. Step 1: creating Bind variable SQL> variable g_sal number Step 2: Invoking the procedure using bind variable SQL> Exec ret_sal( 7900, :g_sal) Step 3: Print the value in the bind variable SQL> Print g_sal Ex 11: ----------------- IN OUT parameters are used to accept the value as well as return the values to the calling environment. Create a procedure which accepts a number and return its square. create or replace procedure cal_square( a In OUT number) is begin a := a*a; end; / To run the above proceure we need to follow a four step process. Step 1: Create Bind variable Step 2: Initiate the Bind variable Step 3: Invoke the procedure using bind varaible Step 4: Print the value in the bind variable Step 1: SQL> Variable n number Step 2: begin :n :=5; end; / Step 3: SQL> Exec cal_square (:n) Step 4: SQL> Print n Ex 12: ------------ To see the list of procedures, use the following queries SQL> select object_name from user_objects where object_type='PROCEDURE'; or SQL> select procedure_name from user_procedures. Ex 13: ------- Using Default keyword: ----------------------- create or replace procedure add_num3( a number, b number default 100, c number default 200) is d number(5); begin d := a+b+c; dbms_output.put_line('The sum is ...'||d); end; / Procedure created. To execute the procedure SQL> EXEC add_num3(10,20,30) Output: The sum is 60 SQL> Exec add_num3(10,20) Output : The sum is 230 Note: Default value is considered if we do not pass any value. SQL> You need to use arrow operator if you pass values to specific parameters Ex: SQL> Exec add_num3(a=>10, c =>20) Output: The sum is 130 Default value 100 is considered for parameter b. ex 14: ---------- If there are any errors in the procedure code, then procedure is created with compilation errors. To see the compilation errors SHO ERR command is used. We need to rectify the errors and recreate the procedure sucessfully. Ex 15: ----------- To see the code of the existing procedure select text from user_source where name =ADD_NUM3; TO drop a procedure: ----------------------- SQL> Drop Procedure <procedure_name>; Ex: SQL> Drop procedure add_num;   Functions:- Function is a PL/SQL block which must and should return single value. Syntax: ----------- Create or replace function <Function_name> ( <Par_name> <mode> <datatype>, ,, ,, ,, ) return datatype is Begin .......... ......... end; / ex1: ----- Create a function which accepts two numbers and display its sum. create or replace function add_num_f1 ( a number, b number) return number is c number(5); begin c :=a+b; return c; end; / To invoke a function from a pl/Sql block: --------------------------------------------- declare n number(5); begin n := add_num_f1(20,40); dbms_output.put_line('The sum is '||n); end; / We can invoke functions from select stmt: ---------------------------------------------- select add_num_f1(30,50) from dual; Functions can be invoked as part of an expression: ---------------------------------------------------- select 100 + add_num_f1(50,10) from dual; Ex2: ------ create a function which accepts sal and returns tax value ( 10% of sal is tax). create or replace function cal_tax ( a number) is begin return a*10/100; end; / Note: A function can return a value using return statement. Ex 3: ---------- Have a look at the following function: create or replace function add_num_f2 ( a number, b number) return number is c number(5); begin insert into dept values (50,'HR','HYDERABAD') c :=a+b; return c; end; / The above function gets created. The above function can be invoked from the pl/SQL block declare n number(5); begin n := add_num_f2(20,40); dbms_output.put_line('The sum is '||n); end; / But, we cannot invoke the above function using select stmt. ex: select add_num_f2(30,50) from dual; -- will give us error. Note: So, functions with dml commands cannot be invoked from select stmt. ------------------------ TO see the list of all the functions select object_name from user_objects where object_type = 'FUNCTION'; ---------------------- To drop a function drop function <function_name>; ex: drop function add_num_f2; ----------------------- Functions are mainly used for calculation purposes. Rest of the activities, prefer procedures.  Packages:- cPackages are logically related sub programs. Package creating involves two steps. Step 1: Creating Package specification (PKS ) Step 2: Creating Package Body ( PKB ) Package Specification: ----------------------------- It contains declaration of sub programs Syntax: ---------- create or replace package <package_name> is declaration of procedures; declaration of functions; end; / Package Body: ----------------------------- It contains definition of sub programs Syntax: ---------- create or replace package body <package_name> is definition of procedures; definition of functions; end; / Ex: ---- Lets create a package with two procedures and function. Procedure add_num -- which takes two parameters and display its sum. Procedure display_emp -- which accepts empno and display ename and sal. Function cal_tax -- which accepts sal and returns tax value (10% of sal is tax value ). Package Specification: --------------------------- create or replace package test_pack is procedure add_num ( a number, b number); procedure display_emp ( l_empno emp.empno%type); function cal_tax ( l_sal emp.sal%type) return number; end test_pack; / Package body: -------------------------- create or replace package body test_pack is procedure add_num ( a number, b number) is c number; begin c := a+b; dbms_output.put_line('The sum is '||c); end; procedure display_emp (l_empno emp.empno%type) is l_ename emp.ename%type; l_sal emp.sal%type; begin select sal into l_sal from emp where empno = l_empno; dbms_output.put_line(l_ename||'.......'||l_sal); end; function cal_tax ( l_sal emp.sal%type) is l_tax number; begin l_tax := l_sal *10/100; return l_tax; end; end test_pack; / To invoke sub programs inside the package: -------------------------------------------- SQL> EXEC test_pack.display_emp (7900) SQL> select empno, ename, sal, test_pack.cal_tax (sal) from emp; Procedure overloading using packages: --------------------------------------- We can achieve procedure overloading using Packages. Basing on the no of parameters and datatype of the parameters, the appropriate procedure is invoked. ex: ---- Create or replace package test_pack2 is procedure p1 ( a number, b number); procedure p1 ( a number); end test_pack2; / create or replace package body test_pack2 is procedure p1 ( a number, b number) is c number; begin c := a+b; dbms_output.put_line('The sum is'||c); end; procedure p1 ( a number) is begin dbms_output.put_line('The square of the number is '||a*a); end; end test_pack2; / In the above package there are two procedures with the same name. Appropriate procedure is invoked basing on the no of parameters which are passed at the time of calling the procedure. Ex: -- SQL> exec test_pack2(10, 20); The sum is 30 SQL> exec test_pack2(10); The square of the number is 100 To drop the package: ---------------------------- We need to drop package bodu first and then the package specification. Drop package body <package_name>; Drop package <package_name>; Ex: ------ Drop package body test_pack2; Drop package test_pack2; Guidelines of the packages: ---------------------------- 1) Helps in modularity of the code. 2) Packages cannot be nested. 3) Packages cannot be parameterized. Triggers:- Trigger is a PL/SQL block which is executed automatically basing on a event. Triggering events: Insert, Update, Delete Trigger timings: Before, after, instead of Syntax: ------ Create or replace trigger <trg_name> <timing> <event> on <table_name> begin ............. ............. ............. end; / ex: ---- create or replace trigger trg1 after insert on dept begin dbms_output.put_line('Thank You'); end; / Trigger Created. Now, when we peroform the event, trigger is executed,. ex: ---- insert into dept values (52,'HR','HYDERABAD'); Thank You 1 row created. We get the message, 'Thank You'. That means trigger is executed. We can create triggers on multiple events. ex: ----- create or replace trigger trg1 after insert or update or delete on dept begin dbms_output.put_line('Thank You'); end; / Trigger created. Now, for all the three events , triggger is fired. ex: -- Update dept set loc='DELHI' where deptno =10; Thank You 1 Row updated. delete from dept where deptno=50; Thank you 1 Row deleted. In the above program, we get the same message for all the events. We can also have different messages to be displayed, basing on the events. Ex: ------ create or replace trigger trg1 after insert or update or delete on dept begin if inserting then dbms_output.put_line('Thank You for inserting'); elsif updating then dbms_output.put_line('Thank You for updating'); else dbms_output.put_line('Thank You for deleting'); end if; end; / Trigger created. In the above program, inserting and updating are the key words which are used to identify the events. Triggers can be classified into two types, basing on the no of times it is executed. 1) Statement level triggers 2) Row level triggers 1) Statement level triggers are executed only once, irrespective of no of rows effected by the event. 2) Row level triggers are executed for every row effected by the event. To create a row level trigger, we need to use for-each-row clause. ex: ----- create or replace trigger trg1 after update on emp for each row begin dbms_output.put_line('Thank you for updating'); end; / Trigger created. update emp set sal=2000 where deptno=10; Thank you for updating Thank you for updating Thank you for updating 3 rows updated. As, the update command is effecting 3 rows, trigger is executed 3 times. These kind of triggers are called row level triggers. Triggers are used to enforce business rules by using :OLD and :NEW qualifiers. ex: ---- Create a trigger which restrict insert operation if sal >5000. create or replace trigger trg1 before insert on emp for each row begin if :new.sal >5000 then raise_application_error(-20150, ' Sal cannot be more than 5000'); end if; end; / Trigger Created. Event: -------- insert into emp( empno, ename,sal, deptno ) values (1111,'ARUN', 6000,10); ERROR: ORA-20150, sal cannot be more than 5000 Ex: -------- Create a trigger which restrict delete operation on emp if job is president. create or replace trigger trg1 before delete on emp for each row begin if :OLD.JOB='PRESIDENT' then raise_application_error(-20151, ' cannot delete president'); end if; end; / Trigger created. Event: --------- delete from emp where ename='KING'; Error: ORA-20151, cannot delete president Instead of triggers: ------------------------ Instead of triggers are helpful to perform DML operations on complex view. Example of complex view: ------------------------ create or replace view emp_dept_v as select e.empno, e.ename, e.sal, e.deptno, d.dname, d.loc from emp e, dept d where e.deptno = d.deptno; View created. Generally, we cannot insert row into complex view. But, by using the instead of triggers, we can do it. ex: ---- create or replace trigger trg1 instead of insert on emp_dept_v for each row begin insert into dept values (:NEW.deptno,:NEW.dname, :NEW.loc); insert into emp ( empno, ename,sal, deptno) values (:NEW.empno, :NEW.ename,:NEW.sal, :NEW.deptno); end; / Trigger Created. Event: ------ insert into emp_dept_v values (2121,'VIJAY',3000,60,'TRAINING','HYDERABAD'); 1 Row created. To see the list of triggers: ------------------------------- select trigger_name from user_triggers; To drop a trigger: ------------------ Drop trigger <trigger_name>; Ex: ----- Drop trigger trg1; Trigger Droped.