Sei sulla pagina 1di 7

Database Engineering - Lab 3

Procedures and Functions:


Example
Step 1:
Please prepare the following tables in order to complete the examples.
You can use the scripts at the end of this document to create tables.
Branch
branchName

assets

branchCity

CIMB

10000

Kuala Lumpur

Hong Leong

20000

Kuala Lumpur

RHB

25000

Kuala Lumpur

HSBC

15000

Johor

Southern

30000

Johor

Customer
customerName

street

customerCity

Jones

Cheras

Kuala Lumpur

Patel

Brickfields

Kuala Lumpur

Smith

Kulim

Penang

Ahmed

Muar

Johor

Chan

Mersing

Johor

Phillp

Bukit Jalil

Kuala Lumpur

Deposit
CustomerName
CSC3064 2016

branchName

accountNumber
1

balance

Jones
Phillip

CIMB
RHB

10
200

1000
1500

Ahmed

Southern

310

4800

Patel

Hong Leong

300

6000

Patel

RHB

220

4500

Chan

HSBC

410

7000

Jones

HSBC

420

4100

Smith

Hong Leong

270

5100

Loan
customerName

branchName

loanNumber

amount

Jones

CIMB

111

30000

Chan

HSBC

120

25000

Ahmed

Southern

130

18000

Smith

Hong Leong

500

50000

Patel
Patel

RHB
Hong Leong

510
610

10000
5000

Step 2
Open a SQL commands window in oracle database home page as
below:

CSC3064 2016

Copy paste the following code


CREATE OR REPLACE PROCEDURE TransferToLoan(theAccountNo INT,
theLoanNo INT, theTransfer INT) AS
BEGIN
UPDATE Deposit SET balance = balance theTransfer WHERE
accountNumber = theAccountNo;
UPDATE Loan SET amount = amount + theTransfer
WHERE loanNumber = theLoanNo;
END;
/
Run (if no error it will create a procedure with given name)
Step 3:
Now you can use object browser to see you procedure and the code as
below, also you can modify the code if you want to by using edit option:

Step 3:
Open Run SQL command line (Command prompt) and connect using the
same username and password:
CSC3064 2016

Then at the SQL> type the following


SQl> TransferToLoan(15, 15, 50000);
Then press enter button, it will show
PL/SQL successfully completed;
Now you can see the results using the following sql query.
select balance from deposit where branchname =RHB;
and
select amount from loan;
You will see the updated results.

FUNCTIONS:
Procedures just have arguments whereas functions have return values as
well as arguments.

Open a SQL commands window in oracle database home page as


below:

Copy paste the following code


CSC3064 2016

CREATE FUNCTION getBalance(accNum INT) RETURN DECIMAL


IS aBalance DECIMAL(8,2);
BEGIN
SELECT balance INTO aBalance FROM Deposit
WHERE accountNumber = accNum;
RETURN aBalance;
END;
Open Run SQL command line (Command prompt) and connect using the
same username and password:
Then at the SQL> type the following
SQl> variable x number
SQL>execute :x:=getBalance(15)
SQL print x;
To call a function in PL/SQL code write:
SQL> CALL getBalance(15) INTO :x;
SQL> print x

Note: The accNUmber must be a number which exist in the


table Deposit.
Exercise: PL/SQL Procedures
Use the Deposit and Loan tables in Oracle for the following questions:
1. Write a single PL/SQL procedure that will add interest to all deposit accounts
(in table Deposit) for a specified bank at a specified rate of interest (enter the
bank name and interest rate as arguments in the procedure argument list).
Run the procedure from SQL*Plus.
2. Edit the procedure from question (1) so that it will output to the screen: the
customer names and new balances for deposit accounts.
Run the stored procedure from SQL*Plus.
3. Write a stored function that will output the total money loaned by a specified
bank (enter the bank name as an argument in the stored function argument
list).
Run the stored function from SQL*Plus.
CSC3064 2016

Customer Table
CREATE TABLE "CUSTOMER"
(

"CUSTNAME" VARCHAR2(30) NOT NULL ENABLE,


"CUSTSTREET" VARCHAR2(30),
"CUSTCITY" VARCHAR2(30),
CONSTRAINT "CUSTOMER_PK" PRIMARY KEY ("CUSTNAME") ENABLE

)
Branch Table
CREATE TABLE "BRANCH"
(

"BRANCHNAME" VARCHAR2(20) NOT NULL ENABLE,


"BRANCHCITY" VARCHAR2(30),
"ASSETS" NUMBER(7,0) DEFAULT 0.00,
"NOCUST" NUMBER,
CONSTRAINT "BRANCH_PK" PRIMARY KEY ("BRANCHNAME") ENABLE

)
Deposit Table
CREATE TABLE "DEPOSIT"
(

"CUSTNAME" VARCHAR2(30),
"BRANCHNAME" VARCHAR2(30),
"ACCNUMBER" NUMBER(7,0) NOT NULL ENABLE,
"BALANCE" NUMBER(7,2),
CONSTRAINT "DEPOSIT_PK" PRIMARY KEY ("ACCNUMBER") ENABLE,
CONSTRAINT "DEPOSIT_FK_BRANCH" FOREIGN KEY ("BRANCHNAME")

REFERENCES "BRANCH" ("BRANCHNAME") ON DELETE CASCADE


ENABLE,
CONSTRAINT "DEPOSIT_FK_CUSTOMER" FOREIGN KEY ("CUSTNAME")
REFERENCES "CUSTOMER" ("CUSTNAME") ON DELETE CASCADE
ENABLE
)

CSC3064 2016

Loan Table
CREATE TABLE "LOAN"
(

"CUSTNAME" VARCHAR2(30),
"BRANCHNAME" VARCHAR2(30),
"LOANNUMBER" NUMBER(8,0) NOT NULL ENABLE,
"AMOUNT" NUMBER(8,2),
CONSTRAINT "LOAN_PK" PRIMARY KEY ("LOANNUMBER") ENABLE,

CONSTRAINT "LOAN_FK_BRANCH" FOREIGN KEY ("BRANCHNAME")


REFERENCES "BRANCH" ("BRANCHNAME") ON DELETE CASCADE
ENABLE,
CONSTRAINT "LOAN_FK_CUSTOMER" FOREIGN KEY ("CUSTNAME")
REFERENCES "CUSTOMER" ("CUSTNAME") ON DELETE CASCADE
ENABLE
)
Sample Bulk Insert
insert all
into branch(branchname,branchcity, assets) values('CIMB','Kuala Lumpur',10000)
into branch(branchname,branchcity, assets) values('Hong Leong','Kuala Lumpur',20000)
into branch(branchname,branchcity, assets) values('RHB','Kuala Lumpur',25000)
into branch(branchname,branchcity, assets) values('HSBC','Johor',15000)
into branch(branchname,branchcity,Assets) values('Southern','Johor',30000)
select * from dual;

CSC3064 2016

Potrebbero piacerti anche