Oracle: Procedures

1,539 views
1,348 views

Published on

Oracle: Procedures

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,539
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Oracle: Procedures

  1. 1. 1<br />Using ORACLE®<br />Procedures and Functions<br />
  2. 2. 2<br />Procedures<br />A 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.<br />Advantages of Procedures:<br />Dividing the program into smaller manageable units<br />Stored in compiled form , hence improve performance.<br />Enable creation of reusable code.<br />DBA can grant/revoke privileges to users to access procedures, hence better security.<br />Reduce network traffic as they are stored in the database .<br />
  3. 3. 3<br />Types of Procedures<br /> There are two kinds of procedures:<br />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. <br />Stored : Unlike anonymous, stored procedures have a unique name assigned to them and are stored in the compiled form in the database.<br /> NOTE: Only Stored procedures can accept parameters and does not use the DECLARE BLOCK.<br />
  4. 4. 4<br />Procedure Syntax<br />SYNTAX:<br />CREATE [/REPLACE] PROCEDURE procedure_name [(parameter datatype),…...]<br />[LANGUAGE { ADA|C|….|SQL}<br />AS<br />Statement 1;<br />…..<br />…… Procedure body. <br />SQL/PLSQL statements<br />….<br />….<br />END;<br />To execute the procedure:<br />EXEC procedure_name;<br />
  5. 5. 5<br />Procedure Example<br />CREATE OR REPLACE PROCEDURE InfoTable_proc<br />AS<br />counter number;<br />c_name varchar2(15);<br />BEGIN<br />DBMS_OUTPUT.PUT_LINE('in order');<br />counter :=10;<br />loop<br />select name into c_name FROM ConTable where ID=counter;<br />DBMS_OUTPUT.PUT_LINE(c);<br />EXIT WHEN counter<1;<br />END LOOP;<br />end;<br />
  6. 6. 6<br />Function<br />A function is a stored sub-routine that returns one value and as only input parameters.<br />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.<br />As functions do not take output parameters it must be assigned to a variable in the program.<br />SYNTAX:<br />CREATE OR REPLACE FUNTION function_name (<parameter list>)<br />RETURN <return_type><br />AS<br />variable declarations if any…<br />…<br />BEGIN<br />Statement1…<br />…<br />END<br />
  7. 7. 7<br />Function Example<br /> CREATE OR REPLACE FUNCTION f1( n IN NUMBER)<br />RETURN NUMBER<br />AS<br /> c NUMBER(4); Variable declaration<br />f NUMBER(4);<br />BEGIN<br /> c:=1;<br /> f:=1;<br />WHILE (c<=n)<br /> LOOP Function Body<br />f:=f*c;<br /> c:=c+1;<br /> END LOOP;<br />RETURN f;<br />END;<br /> This function when executed calculates the factorial of the parameter n.<br />
  8. 8. 8<br />Parameters<br />Both PROCEDURE or FUNCTION take different parameters. There are three different types of parameters :<br />
  9. 9. THANK YOU<br />9<br />THANK YOU FOR VIEWING THIS PRESENTATION<br />FOR MORE PRESENTATIONS AND VIDEOS ON ORACLE AND DATAMINING ,<br />please visit: <br />www.dataminingtools.net<br />

×