1Using ORACLE®Procedures and Functions
2ProceduresA procedure is a collection  of  SQL and procedural statements that perform a specific task and are assigned a unique name within the schema and stored in the  database.Advantages of Procedures:Dividing the program into smaller manageable unitsStored in compiled form , hence improve performance.Enable creation of reusable code.DBA can grant/revoke privileges to users to access procedures, hence better security.Reduce network traffic as they are stored in the database .
3Types of Procedures There are two kinds of procedures:Anonymous: These procedures do not have a name assigned to them. It is complied each time when the user submits its source code to the database server. Stored : Unlike anonymous, stored procedures have a unique name assigned to them and are stored in the compiled form in the database.	   NOTE: Only Stored procedures can accept parameters and does not use the 				DECLARE BLOCK.
4Procedure SyntaxSYNTAX:CREATE [/REPLACE] PROCEDURE procedure_name [(parameter datatype),…...][LANGUAGE { ADA|C|….|SQL}ASStatement 1;…..……			    Procedure body.	SQL/PLSQL statements….….END;To execute the procedure:EXEC procedure_name;
5Procedure ExampleCREATE OR REPLACE PROCEDURE InfoTable_procAScounter number;c_name varchar2(15);BEGINDBMS_OUTPUT.PUT_LINE('in order');counter :=10;loopselect name into c_name FROM ConTable where ID=counter;DBMS_OUTPUT.PUT_LINE(c);EXIT WHEN counter<1;END LOOP;end;
6FunctionA function is a stored sub-routine that returns one value and as only input parameters.A stored function is same as a procedure except for the procedure keyword is replaced by the keyword function and it carries out a specific operation and returns a value.As functions do not take output parameters it must be assigned to a variable in the program.SYNTAX:CREATE OR REPLACE FUNTION function_name (<parameter list>)RETURN <return_type>ASvariable declarations if any……BEGINStatement1……END
7Function Example CREATE OR REPLACE FUNCTION f1( n IN NUMBER)RETURN NUMBERAS	c NUMBER(4);			Variable declarationf NUMBER(4);BEGIN	c:=1;	f:=1;WHILE (c<=n)	LOOP				  Function Bodyf:=f*c;		c:=c+1;	END LOOP;RETURN f;END;	    This function when executed calculates the factorial of the parameter n.
8ParametersBoth PROCEDURE or FUNCTION take different parameters. There are three different types of parameters :
THANK YOU9THANK YOU FOR VIEWING THIS PRESENTATIONFOR MORE PRESENTATIONS AND VIDEOS ON ORACLE AND DATAMINING ,please visit:  www.dataminingtools.net

Oracle: Procedures

  • 1.
  • 2.
    2ProceduresA procedure isa collection of SQL and procedural statements that perform a specific task and are assigned a unique name within the schema and stored in the database.Advantages of Procedures:Dividing the program into smaller manageable unitsStored in compiled form , hence improve performance.Enable creation of reusable code.DBA can grant/revoke privileges to users to access procedures, hence better security.Reduce network traffic as they are stored in the database .
  • 3.
    3Types of ProceduresThere are two kinds of procedures:Anonymous: These procedures do not have a name assigned to them. It is complied each time when the user submits its source code to the database server. Stored : Unlike anonymous, stored procedures have a unique name assigned to them and are stored in the compiled form in the database. NOTE: Only Stored procedures can accept parameters and does not use the DECLARE BLOCK.
  • 4.
    4Procedure SyntaxSYNTAX:CREATE [/REPLACE]PROCEDURE procedure_name [(parameter datatype),…...][LANGUAGE { ADA|C|….|SQL}ASStatement 1;…..…… Procedure body. SQL/PLSQL statements….….END;To execute the procedure:EXEC procedure_name;
  • 5.
    5Procedure ExampleCREATE ORREPLACE PROCEDURE InfoTable_procAScounter number;c_name varchar2(15);BEGINDBMS_OUTPUT.PUT_LINE('in order');counter :=10;loopselect name into c_name FROM ConTable where ID=counter;DBMS_OUTPUT.PUT_LINE(c);EXIT WHEN counter<1;END LOOP;end;
  • 6.
    6FunctionA function isa stored sub-routine that returns one value and as only input parameters.A stored function is same as a procedure except for the procedure keyword is replaced by the keyword function and it carries out a specific operation and returns a value.As functions do not take output parameters it must be assigned to a variable in the program.SYNTAX:CREATE OR REPLACE FUNTION function_name (<parameter list>)RETURN <return_type>ASvariable declarations if any……BEGINStatement1……END
  • 7.
    7Function Example CREATEOR REPLACE FUNCTION f1( n IN NUMBER)RETURN NUMBERAS c NUMBER(4); Variable declarationf NUMBER(4);BEGIN c:=1; f:=1;WHILE (c<=n) LOOP Function Bodyf:=f*c; c:=c+1; END LOOP;RETURN f;END; This function when executed calculates the factorial of the parameter n.
  • 8.
    8ParametersBoth PROCEDURE orFUNCTION take different parameters. There are three different types of parameters :
  • 9.
    THANK YOU9THANK YOUFOR VIEWING THIS PRESENTATIONFOR MORE PRESENTATIONS AND VIDEOS ON ORACLE AND DATAMINING ,please visit: www.dataminingtools.net