Vous êtes sur la page 1sur 11

Roll No: TI 02 Assignment Number 6

Batch: A

Problem Statement: Study of PL/SQL procedure and functions.

Objective: To understand the concept of Procedure and function, its


difference in PL/SQL programming.

Theory:

What is procedure and function?

Procedure:

1. Procedure can return multiple values (max 1024).

2. Procedures are normally used for executing business logic

3. A Procedure may return one or more values through parameters or


may not return at all.

4. Procedure can not be called from the SQL statements.

5. Procedure cannot be used in select or update or delete statement.

6. Procedure cannot be called inside select statement.

7. Procedures are not considered expressions

Functions:

1. A Function returns only 1 value.

2. Functions are normally used for computations.

3. A Function always returns a value using the return statement.

4. Function can be called from SQL statements.

5. Functions can be used in select or update or delete statement.

6. Functions can be called inside select statement.

7. Functions are considered expressions.

What is stored procedure?

1. A stored procedure is a subroutine.

2. It is available to applications accessing a relational database


system.

3. Stored procedures are sometimes called a proc, sproc, StoPro, or


SP

4. They are actually stored in the database data dictionary.


5. Large or complex processing that might require the execution of
several SQL statements is moved into stored procedures

Syntax for procedure.

Declaring a Procedure

CREATE OR REPLACE

PROCEDURE procedure_name ( parameters ) IS

BEGIN

procedure_body

END;

TO Call Procedure:

BEGIN

Procedure_name();

END;

Drop Procedure:

DROP PROCEDURE procedure_name;

Syntax for function.

Declaring Function:

CREATE OR REPLACE

FUNCTION function_name (function_parameters)

RETURN return_type IS

BEGIN

function_body

RETURN something_of_return_type;

END;

To call a function:

BEGIN

DBMS_OUTPUT.PUT_LINE(’RESULT IS: ’ ||ADD_TWO(12,34));

END;
Drop a Function:

DROP FUNCTION function_name;

What is IN, OUT & IN OUT parameter.

IN:

It is used as the data type for the function/procedure parameters


when we want to accept the input for the operations.

Out:

It is used as the data type for the function/procedure


parameters when we want to display the result.

IN OUT:

It is used as the data type for the function/procedure


parameters when we want to accept as well as display the result.

How to display procedure/function list.

Function:

SELECT OBJECT_NAME

FROM USER_OBJECTS

WHERE OBJECT_TYPE = ’FUNCTION’;

Procedure:

SELECT OBJECT_NAME

FROM USER_OBJECTS

WHERE OBJECT_TYPE = ’Procedure’;

Implementation:

Platform: Windows Operating System

Technology : Oracle 10g

Input: User Input.

Output : Result based on procedure / function execution.


// PROCEDURES

 Reverse of a number

SQL> create or replace procedure reverse(n in int) is

2 temp int;

3 b int;

4 begin

5 temp:=n;

6 dbms_output.put_line('Reverse of number '||n || ' is');

7 while temp>=1

8 loop

9 b:=temp mod 10;

10 temp:=temp/10;

11 dbms_output.put_line(b);

12 end loop;

13 end;

14 /

Procedure created.

SQL> declare

2 s int;

3 begin

4 s:=3214;

5 reverse(s);

6 end;

7 /

Reverse of number 3214 is

PL/SQL procedure successfully completed.


 Square of a number

SQL> create or replace procedure square(n in int ) is

2 a int;

3 begin

4 a:=n*n;

5 dbms_output.put_line('Square of the number is: '|| a);

6 end;

7 /

Procedure created.

SQL> declare

2 a int;

3 begin

4 a:=&a;

5 square(a);

6 end;

7 /

Enter value for a: 6

old 4: a:=&a;

new 4: a:=6;

Square of the number is: 36

PL/SQL procedure successfully completed.

SQL> /

Enter value for a: 12

old 4: a:=&a;

new 4: a:=12;

Square of the number is: 144

PL/SQL procedure successfully completed.


 Sum of a digits present in a number

SQL> create or replace procedure sum_d(n in int ) is

2 c int;

3 b int;

4 s int;

5 begin

6 c:=n;

7 s:=0;

8 while c>0

9 loop

10 b:=c mod 10;

11 s:=s+b;

12 c:=c/10;

13 end loop;

14 dbms_output.put_line('sum of digit is: '||s);

15 end;

16 /

Procedure created.

SQL> declare

2 n int;

3 begin

4 n:=&n;

5 sum_d(n);

6 end;

7 /

Enter value for n: 1234

old 4: n:=&n;

new 4: n:=1234;

sum of digit is: 10

PL/SQL procedure successfully completed.


//FUNCTIONS

SQL> set serveroutput on;

SQL> create table area(empid number(4),name varchar(20),dept


varchar(20),salary number(10));

Table created.

SQL> select * from employee;

EMPID NAME DEPT SALARY

---------- -------------------- -------------------- ----------

1 Ashish Debugging 100000

2 Apoorv Testing 60000

3 Abhishek Designing 80000

4 Alok Product 70000

7 Prachi Software 150000

8 Pranjal Hardware 30000

9 Waqar Gaming 15000

10 Kalpesh Coding 200000

8 rows selected.

 To Delete an Record from Employee Table

SQL> create or replace function delt(eid int)

2 return int is

3 cnt int;

4 i int :=1;

5 flag int:=0;

6 begin

7 select count(*) into cnt from employee;


8 for i in 1.. cnt

9 loop

10 if(eid=i) then

11 delete from employee where empid=eid;

12 flag:=1;

13 end if;

14 end loop;

15 return flag;

16 end;

17 /

Function created.

SQL> declare

2 eid int(2);

3 flag int(2);

5 begin

6 eid:=&eid;

7 flag:= delt(eid);

8 if(flag=1) then

9 dbms_output.put_line(' Record Deleted Successfully!!!');

10 else

11 dbms_output.put_line(' Record Not Deleted!!!');

12 end if;

13 end;

14 /

Enter value for eid: 7

old 6: eid:=&eid;

new 6: eid:=7;

Record Deleted Successfully!!!

PL/SQL procedure successfully completed.


SQL> /

Enter value for eid: 0

old 6: eid:=&eid;

new 6: eid:=0;

Record Not Deleted!!!

PL/SQL procedure successfully completed.

SQL> select * from employee;

EMPID NAME DEPT SALARY

---------- -------------------- -------------------- ----------

1 Ashish Debugging 100000

2 Apoorv Testing 60000

3 Abhishek Designing 80000

4 Alok Product 70000

8 Pranjal Hardware 30000

9 Waqar Gaming 15000

10 Kalpesh Coding 200000

7 rows selected.

 To Find Employee Salary High, Medium or Low from Employee Table

SQL> create or replace function sal(eid int)

2 return int is

3 sal int;

4 flag int;

5 begin

6 select salary into sal from employee where empid=eid;

7 if(sal>100000) then

8 flag:=3;

9 elsif( sal>50000 and sal<=100000) then

10 flag:=2;
11 elsif(sal<=50000) then

12 flag:=1;

13 end if;

14 return flag;

15 end;

16 /

Function created.

SQL> declare

2 eid int(2);

3 flag int(2);

5 begin

6 eid:=&eid;

7 flag:= sal(eid);

8 if(flag=3) then

9 dbms_output.put_line(' High Salary!!!');

10 elsif(flag=2) then

11 dbms_output.put_line(' Medium Salary!!!');

12 else

13 dbms_output.put_line('Low Salary!!!');

14 end if;

15 end;

16 /

Enter value for eid: 4

old 6: eid:=&eid;

new 6: eid:=4;

Medium Salary!!!

PL/SQL procedure successfully completed.


 To Find average salary of all Employees from Employee Table

SQL> create or replace function av(sal out int)

2 return int is

3 begin

4 select avg(salary) into sal from employee;

5 return sal;

6 end;

7 /

Function created.

SQL> declare

2 salary int(10);

3 eid int;

4 begin

5 salary:=av(salary);

6 dbms_output.put_line('Average Salary is ' ||salary);

7 end;

8 /

Average Salary is 79286

PL/SQL procedure successfully completed.

Conclusion: Studied how to use procedure and function in PL/SQL.

Vous aimerez peut-être aussi