Oracle Procedures

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

A procedure is a group of PL/SQL statements that can be called by name. The call specification (sometimes called call spec) specifies a java method or a third-generation language routine so that it can be called from SQL and PL/SQL.

Create Procedure

Syntax

  1. CREATE [OR REPLACE] PROCEDURE procedure_name  
  2.     [ (parameter [,parameter]) ]  
  3. IS  
  4.     [declaration_section]  
  5. BEGIN  
  6.     executable_section  
  7. [EXCEPTION  
  8.     exception_section]  
  9. END [procedure_name];  

Following are the three types of procedures that must be defined to create a procedure.

  • IN: It is a default parameter. It passes the value to the subprogram.
  • OUT: It must be specified. It returns a value to the caller.
  • IN OUT: It must be specified. It passes an initial value to the subprogram and returns an updated value to the caller.

Oracle Create procedure example

In this example, we are going to insert record in the "user" table. So you need to create user table first.

Table creation:

  1. create table user(id number(10) primary key,name varchar2(100));  

Now write the procedure code to insert record in user table.

Procedure Code:

  1. create or replace procedure "INSERTUSER"    
  2. (id IN NUMBER,    
  3. name IN VARCHAR2)    
  4. is    
  5. begin    
  6. insert into user values(id,name);    
  7. end;    
  8. /       

Output:

Procedure created.

Oracle program to call procedure

Let's see the code to call above created procedure.

  1. BEGIN    
  2.    insertuser(101,'Rahul');  
  3.    dbms_output.put_line('record inserted successfully');    
  4. END;    
  5. /    

Now, see the "USER" table, you will see one record is inserted.

ID Name
101 Rahul

Oracle Drop Procedure

Syntax

  1. DROP PROCEDURE procedure_name;   

Example to drop procedure

  1. DROP PROCEDURE pro1;