0% found this document useful (0 votes)
77 views

Stored Procedure

Stored procedures allow for code reuse through SQL statements that are compiled and optimized. They are more efficient than raw SQL especially for repeated executions as the execution plan is cached. Stored procedures are created using CREATE PROCEDURE and parameters can be passed in for flexibility. They provide a way to share common database operations across users and applications through a standardized interface.

Uploaded by

Ruturaj Sawant
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Stored Procedure

Stored procedures allow for code reuse through SQL statements that are compiled and optimized. They are more efficient than raw SQL especially for repeated executions as the execution plan is cached. Stored procedures are created using CREATE PROCEDURE and parameters can be passed in for flexibility. They provide a way to share common database operations across users and applications through a standardized interface.

Uploaded by

Ruturaj Sawant
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

A stored procedure consists of one or more SQL statements that perform a unit of work, that are compiled into

an execution plan when executed. An execution plan contains two parts;


part that contains the code to be executed and one part that contains the variables and parameters that are to be used by the code.

Stored procedures are more efficient than SQL statements, when executed from a program.
when the stored procedure is created and saved it is compiled. During the compilation process, SQL creates and optimizes an execution plan for the stored procedure.

once a stored procedure is executed, it is placed in SQL cache.


subsequent executions are executed from cache, which also provides improved performance.

Stored procedures provide a way to share code


Multiple users and programs can execute the same stored procedures, thus providing code reuse.

Stored procedure maintenance becomes easier


we can change one stored procedure and it immediately becomes effective for all users and programs.

The creation of a procedure starts with the CREATE PROCEDURE expression. You can also use CREATE PROC. Syntax CREATE PROCEDURE ProcedureName AS Body of the Procedure The section, group of words, or group of lines after the AS keyword is called the body of the procedure.

Creating a Procedure

Executing a Procedure
To execute a procedure, you use the EXECUTE keyword followed by the name of the procedure. Syntax
EXECUTE ProcedureName EXEC ProcedureName

Simple Procedure
SQL> set serveroutput on; create or replace procedure proc1 2 as 3 begin 4 dbms_output.put_line('hello'); 5 end; 6 /

Procedure created.
SQL> execute proc1; hello PL/SQL procedure successfully completed. SQL> exec proc1; hello

Passing Arguments
Syntax CREATE PROCEDURE ProcedureName(var_namedatatype) As Begin delete from emp1 where emp_no=eno; end; / How we can execute parametarised procedure exec proc1(110);

Example
create or replace procedure proc2(esal number) as begin update emp1 set dep_no=40 where salary=esal; end; / exec proc1(99000);

create or replace procedure proc2(esal number) as begin if esal>85000 then dbms_output.put_line('sorry salary is not available'); end if; end; / execute proc2(86000);

Procedures taking more than 1 parameter


create or replace procedure proc3(eno integer,sal integer) IS begin update emp1 set salary=sal where emp_no=eno; end; / execute proc3(101,98000);

create or replace procedure proc2(esal number) IS begin if esal>85000 then update emp1 set salary=esal where salary>90000; end if; end;

Procedure taking argument value using & operator.


create or replace procedure proc3 as eno integer; sal integer; begin update emp1 set salary=&sal where emp_no=&eno; end; / exec proc3;

Example:
CREATE OR REPLACE PROCEDURE in_param (mesg VARCHAR2) IS BEGIN dbms_output.put_line(mesg); END in_param; / exec in_param('hello good morning')

OUT Operator
>CREATE OR REPLACE PROCEDURE out_param(mesg OUT VARCHAR2) IS BEGIN mesg := 'Single OUT Parameter'; END out_param; / >set serveroutput on >DECLARE s VARCHAR2(50); BEGIN out_param(s); dbms_output.put_line(s); END;

Deleting a Procedure Syntax


DROP PROCEDURE ProcedureName

You might also like