# Shantanu Gontia

120913010, MTech-CSE

PL/SQL: Basic Programming Constructs, Exception Handling and Stored Subprograms
Submitted by: Shantanu Gontia Reg No 120913010 MTech CSE, 1st Year

1. Write a Pl/Sql block to compute the value of 10! (i.e. 10*9*8*7*6*5*4*3*2*1), without and with looping constructs. A. With Loop: declare cnt number; num number:=1; result number(3); begin for cnt in 1..10 loop num:= num*(cnt); end loop; dbms_output.put_line('Factorial 10 is: '|| num); end;

1|Page

Shantanu Gontia B. Without Loop:

120913010, MTech-CSE

declare cnt number:=1; num number:=1; result number(3); begin num:= num*(cnt)*(cnt+1)*(cnt+2)*(cnt+3)*(cnt+4)*(cnt+5)*(cnt+6)*(cnt+7)*(cnt+8)*(cnt+9); dbms_output.put_line('Factorial 10 is: '|| num); end;

2. Write a PL/SQL procedure that accepts a single positive number as input, and prints out the factorial of that number. Result: create procedure factorial(input in number) is cnt number; result number:=1; begin for cnt in 1..input loop result:=result*(cnt); end loop; dbms_output.put_line('Factorial of ' ||input|| 'is: '|| result); end; execute factorial(3);

2|Page

Shantanu Gontia

120913010, MTech-CSE

3. Handle the division-by-zero exception in the invert function which computes the given number. Ans: CREATE OR REPLACE FUNCTION INVERTT RETURN NUMBER IS FIRST number(5,4); SECOND number(5); BEGIN dbms_output.put_line('-----PROGRAM TO INVERT NUMBER----'); FIRST:=1/&SECOND; dbms_output.put_line('INVERTED NUMBER IS----'||FIRST); RETURN(FIRST); EXCEPTION WHEN ZERO_DIVIDE then dbms_output.put_line('YOU ARE DIVIDING BY ZERO'); END INVERTT;

inverse of

3|Page

Shantanu Gontia

120913010, MTech-CSE

4. Handle no-data-found exception in a simple procedure, balance( CName IN VARCHAR2, Balance OUT NUMBER) that prints the balance for a specified customer from the simple banking database. Ans: create or replace procedure bal_chk1( c_name in varchar2, balance out number) is bal number; accnt_num number; begin select a.account_number,balance into accnt_num, bal from account a, depositor d where a.account_number = d.account_number and d.customer_name = c_name; balance:= bal; dbms_output.put_line('Balance for the user is : ' || balance); EXCEPTION WHEN NO_DATA_FOUND THEN dbms_output.put_line('No Data Found'); end;

4|Page

Shantanu Gontia

120913010, MTech-CSE

5. Give a procedure on banking database to include too-many-rows exception. Ans: CREATE OR REPLACE PROCEDURE MANYROWS(FIRST IN NUMBER,SECOND OUT number) IS BEGIN SELECT EMPNO INTO SECOND FROM EMP WHERE SAL=FIRST; dbms_output.put_line('YOU ARE EMPLOYEE NUMBER---'||SECOND); EXCEPTION WHEN TOO_MANY_ROWS then dbms_output.put_line('THERE ARE MANY OUTCOME'); END;

5|Page

Shantanu Gontia

120913010, MTech-CSE

6. Write a “safe” withdrawal procedure, withdrawal( AccNo IN VARCHAR2, Amount IN NUMBER) on Account table. Check for sufficient balance and update the balance. Use an exception to handle the case of attempting to overdraw an account. Ans: create or replace procedure withdraw( accnt_numb in varchar2, amount in number) is accnt_balance number; overdraw_amount EXCEPTION; begin select balance into accnt_balance from account where account_number = accnt_numb; if amount > accnt_balance then raise overdraw_amount; else accnt_balance:= accnt_balance - amount; update account set balance = accnt_balance where account_number = accnt_numb; dbms_output.put_line(‘Amount withdrawn successfully ‘); end if; EXCEPTION 6|Page

Shantanu Gontia WHEN overdraw_amount THEN dbms_output.put_line('Insufficient balance'); WHEN NO_DATA_FOUND THEN dbms_output.put_line('No Data Found'); end;

120913010, MTech-CSE

7. Write

transfer(giveAccNo IN VARCHAR2, receiveAccNor IN VARCHAR2,amount IN NUMBER) to transfer money from one person's account to another. Check for the transfer amount being negative and check whether the giver has sufficient

a

procedure,

funds to allow the transfer. CREATE OR REPLACE PROCEDURE TRANSFER(FIRST IN NUMBER,SECOND IN number,THIRD IN NUMBER) IS FOURTH NUMBER(5); BEGIN SELECT EMPNO INTO FOURTH FROM EMP WHERE EMPNO=FIRST; dbms_output.put_line('YOU ARE EMPLOYEE NUMBER---'||FIRST); UPDATE EMP SET SAL = SAL-THIRD WHERE EMPNO=FIRST; UPDATE EMP SET SAL = SAL+THIRD WHERE EMPNO=SECOND; 7|Page

Shantanu Gontia EXCEPTION WHEN TOO_MANY_ROWS then dbms_output.put_line('THERE ARE MANY OUTCOME'); END;

120913010, MTech-CSE

8. Create a package by including few procedures / functions defined above and demonstrate the usage of the same.

create or replace PACKAGE pck_transact as procedure withdraw(accnt_numb in varchar2,amount in number); procedure bal_chk1( c_name in varchar2, balance out number); function fun_display return number; end; /

8|Page

Shantanu Gontia

120913010, MTech-CSE

Package body : create or replace package body pck_transact as procedure withdraw( accnt_numb in varchar2, amount in number) is accnt_balance number; overdraw_amount EXCEPTION; begin select balance into accnt_balance from account where account_number = accnt_numb; if amount > accnt_balance then raise overdraw_amount; else accnt_balance:= accnt_balance - amount; update account set balance = accnt_balance where account_number = accnt_numb; dbms_output.put_line('Amount withdrawn successfully'); end if; EXCEPTION WHEN overdraw_amount THEN dbms_output.put_line('Insufficient balance'); WHEN NO_DATA_FOUND THEN dbms_output.put_line('No Data Found'); end; procedure bal_chk1( c_name in varchar2, balance out number) is bal number; accnt_num number; begin select a.account_number,balance into accnt_num,bal from account a, depositor d where a.account_number = d.account_number and d.customer_name = c_name; balance:= bal; dbms_output.put_line('Balance for the user is : ' || balance); EXCEPTION WHEN NO_DATA_FOUND THEN dbms_output.put_line('No Data Found'); end; function fun_display return number is customer_id number; begin select cust_id into customers_id from customers where cust_name='shaan'; 9|Page

Shantanu Gontia return customer_id; end fun_display; end; /

120913010, MTech-CSE

10 | P a g e