Skip to content
Related Articles

Related Articles

PL/SQL Transactions
  • Last Updated : 05 Dec, 2018

Prerequisites – PL/SQL Introduction, PL/SQL | User Input
Write a PL/SQL code that will accept an account number from user. Check if user balance is less than minimum balance than only deduct Rs 100 from balance. The process is fired on the acct_mstr.

Example – Consider the following scenario,

acct_master (acct_no number(5) primary key, 
                         acct_name varchar2(10), 
                            balance number(10)); 

First, you need to create table acct_master,

# CREATING table acct_master 
create table acct_master(acct_no number(5) primary key, 
                                acct_name varchar2(10), 
                                   balance number(10)); 

Insert these data into the table,

# INSERTING data in acct_mstr
insert into acct_master values(1, 'aaa', 1000)
insert into acct_master values(2, 'bbb', 100)
insert into acct_master values(3, 'ccc', 1100)
insert into acct_master values(4, 'ddd', 700)
insert into acct_master values(5, 'eee', 1700) 

Approach used –



  • Step-1: Declare variables and set minimum balance in one of variable.
  • Step-2: Take account number input from user.
  • Step-3: Select the balance of that user into one variable.
  • Step-4: Check if balance is less than minimum balance or not.
  • Step-5: If balance is less, then update the balance in table with balance = balance -100, and display the balance after deducting 100 from that variable in which balance was stored. Then, display the output.
  • Step-6: Else simply print the value.

Note that all text in green colour are comments.

Below is the required implementation:




-- DECLARING VARIABLES
DECLARE 
xacct_no number(5);
  
-- here, minimum balance is set to 1000;
xmin_bal number(5):=1000; 
xbalance number(5);
  
BEGIN
  
-- taking input from user
xacct_no:=&xacct_no; 
  
-- selecting balance of that user INTO "xbalance";
select balance into xbalance 
from acct_master 
where acct_no=xacct_no; 
  
-- if condition true, updating balance 
-- with balance = balance - 100 
IF(xbalance < xmin_bal) THEN --condition check
update acct_master 
set balance=balance-100 
where acct_no=xacct_no; 
                          
-- remaining amount                                                                 
xbalance:=xbalance-100; 
dbms_output.put_line('Rs 100 is deducted 
               and current balance is '||xbalance);
  
-- if condition is false 
ELSE             
dbms_output.put_line('Current balance is '||xbalance);
  
--ENDING IF 
END IF;    
  
-- ENDING OF BEGIN
END
  
/     -- FOR DISPLAYING OUTPUT IN SCREEN

Output:

Enter value for xacct_no: 2
old 6: xacct_no:=&xacct_no;
new 6: xacct_no:=2;
Rs 100 is deducted and current balance is 0

PL/SQL procedure successfully completed.

SQL> /
Enter value for xacct_no: 3
old   6: xacct_no:=&xacct_no;
new   6: xacct_no:=3;
Current balance is 1100

PL/SQL procedure successfully completed. 
My Personal Notes arrow_drop_up
Recommended Articles
Page :