0% found this document useful (0 votes)
88 views5 pages

Create or Replace Function Fact (N Number) Return Number Is I NUMBER (10) F Number: 1 Begin For I in 1.. N Loop F: F I End Loop Return F End

This document contains examples of PL/SQL functions and procedures. The functions include creating a factorial function, a string reversal function, and a function to check if an item ID exists. The procedures include increasing all employee salaries, updating account balances and transaction tables for fixed deposits, and increasing an employee's salary based on their job designation.

Uploaded by

jyothimidhuna
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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views5 pages

Create or Replace Function Fact (N Number) Return Number Is I NUMBER (10) F Number: 1 Begin For I in 1.. N Loop F: F I End Loop Return F End

This document contains examples of PL/SQL functions and procedures. The functions include creating a factorial function, a string reversal function, and a function to check if an item ID exists. The procedures include increasing all employee salaries, updating account balances and transaction tables for fixed deposits, and increasing an employee's salary based on their job designation.

Uploaded by

jyothimidhuna
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 DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

FUNCTIONS

1. Write a function to create factorial of a number.


CREATE OR REPLACE FUNCTION FACT (N NUMBER) RETURN NUMBER IS
I NUMBER(10);
F NUMBER :=1;
BEGIN
FOR I IN 1.. N LOOP
F:= F*I;
END LOOP;
RETURN F;
END;
SQL> /
Function created.
SQL> select fact(8) from dual;
FACT(8)
---------
40320
2. Write a PL/SQL code that update the stock in the Item_master table each time a transaction
takes place in the transaction table. The change in the item_master table depends on the
itemid.If the itemid is present in the item_master table then an update operation is (stock=stock-
qty) made. If itemid is not present in the item_master table then a record is inserted into the
item_master table.
A stored function is used to perform the itemid check operation. The function accepts itemid
variable.
Table Name: Item_master
itemid number primarykey
itemname varchar Item Description
itemstock number Balance stock for an Item
Table Name: Transaction
itemid Number(3) primarykey
itemname varchar Item Description
Qty number Qty required

PROGRAM
create function fun (vid in number) return number IS
no number (4);
begin
select itemid into no from item_master where itemid=vid;
return 1;
Exception
When NO_Data_Found then
return 0;
end;
set serveroutput on
declare
cursor cur IS select itemid, itemname, qty from transaction;
id transaction.itemid%type;
n transaction.itemname%type;
q transaction.itemqty%type;
V number (1);
begin
open cur;
loop
fetch cur into id, n, q;
exit when cur%notfound;
V: =fun (id);
if v=0 then
insert into item_master values (id, n, q);
dbms_output.put_line ('v is 0');
else
update item_master set stock=stock-q where itemid=id;
dbms_output.put_line ('v is 1');
end if;
end loop;
close cur;
commit; end;
3. Create a user defined function for reversing a string.
Create or replace function string_reverse (string varchar) return varchar is
rev varchar(20);
len number(3);
Begin
len:=length(string);
For I in REVERSE 1..len
Loop
rev:=rev||substr(string,I,len);
End loop;
Return(rev);
End string_reverse;
PROCEDURES
1. Write a Procedure to increase the salary for all the employees in the EMP table :
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
--------- ---------- --------- --------- --------- --------- --------- ---------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
14 rows selected.
SQL> create or replace procedure inc(i number)
is
begin
update emp set sal =sal+i;
end;
/
Procedure created.
To execute the procedures in the PL/SQL block:
SQL> declare
begin
inc(100);
end;
/
PL/SQL procedure successfully completed.
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1700 300 30
14 rows selected.
2.Create a procedure and stored in the database.This procedure when called in a PL/SQL block
update the current balance in the AC_MASTER table and status in another table named
FD_DTLS. It also inserts an entry to register the credit of the fixed deposit amount in the
TRANS_MSTR table.
PROGRAM
create procedure pr (vfd_no in varchar2,vacno in varchar 2,vamt in number)is
bal number;
begin
select curbal into bal from acct_mstr where ac_no=vac_no ;
insert into trans_mstr values(trans_no,ac_no,dt,type,particular,dr_cr,amt,bal);
update ac_mstr set curbal=curbal+vamt where ac_no=vac_no;
update fd_dtls set status='M' where fd_n0=vfd_no;
end;
set serveroutput on
declare
cursor c is select fd_no,pay_acno,dueamt from fd_dtls where to_char(duedt,'dd-mm-
yy')=to_char(sysdate,'dd-mm-yy') and status='A' and pay_acno is not null;
mfd_no varchar(10);
mpay_acno varchar(10);
mamt number(10);
mstate number:=0;
begin
open c;
loop
fetch c into mfd_no,mpay_acno,mamt;
exit whenc%notfound;
proc(mfd_no,mpay_acno,mamt);
mstate:=1;
end loop;
if mstate=0 then
dbms_output.put_line(there are no fixed deposit due for payment);
end if;
close c;
commit;
end;
3. Write a procedure to increase the salary for the specified employee. Using empno in the emp
table based on the following criteria: increase the salary by 5% for clerks, 7% for salesman, 10%
for analyst, 20 % for manager and 25% for president. Activate using pl/sql block.
SQL> SELECT * FROM EMP;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPT
7369 SMITH CLERK 7902 17-DEC-80 800 20
create or replace procedure designation(eno number) is
begin
update emp set sal=sal+sal*5/100 where job ='clerk' and empno=eno;
update emp set sal=sal+sal*7/100 where job='salesman' and empno=eno;
update emp set sal=sal+sal*10/100 where job='analyst' and empno=eno;
update emp set sal=sal+sal*20/100 where job='manager' and empno=eno;
update emp set sal=sal+sal*25/100 where job='president' and empno=eno;
end;
SQL> /
Procedure created.
SQL> DECLARE
2 BEGIN
3 DESIGNATION(7369);
4 END;
5/
PL/SQL procedure successfully completed.
SQL> SELECT * FROM EMP;
 

You might also like