Stored Procedure
Stored Procedure
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.
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);
create or replace procedure proc2(esal number) IS begin if esal>85000 then update emp1 set salary=esal where salary>90000; end if; end;
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;