Sie sind auf Seite 1von 4

OUTPUT

SQL> Rem 1. For the given customer number, display as "No Account
for the given customer <number>" if the customer doesn't have any
account. If the customer has more than
SQL> Rem one account, display as "The given customer <number> have
more than one Account". If the customer has only one account, then
display as "The given customer
SQL> Rem <number> holds exactly one Account". Use predefined
exception handling.
SQL>
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
2 customer_id varchar2(5) := '&customer_id';
3 cnt number;
4 ex1 exception;
5 ex2 exception;
6 ex3 exception;
7 BEGIN
8 select count(*) into cnt from ACCOUNT where
ACCOUNT.CID=customer_id;
9 IF cnt=0 THEN
10 RAISE ex1;
11 ELSIF cnt>1 THEN
12 RAISE ex2;
13 ELSIF cnt=1 THEN
14 RAISE ex3;
15 END IF;
16 EXCEPTION
17 WHEN ex1 THEN
18 dbms_output.put_line('No account for the given customer
!');
19 WHEN ex2 THEN
20 dbms_output.put_line('The given customer has more than
one Account');
21 WHEN ex3 THEN
22 dbms_output.put_line('The given customer holds exactly
one Account');
23 END;
24 /

Enter value for customer_id: 100


old 2: customer_id varchar2(5) := '&customer_id';
new 2: customer_id varchar2(5) := '100';
The given customer has more than one Account

PL/SQL procedure successfully completed.

SQL> Rem 2. Use stored procedure to insert the values into


transaction relation. While populating, raise an user-defined
exception when the transaction date (tdate) is more than the
SQL> Rem todays date.
SQL>
SQL>
SQL> SET SERVEROUTPUT ON;
SQL> CREATE OR REPLACE PROCEDURE Trans(a IN varchar2, b IN varchar2,
c IN varchar2, d IN date,e IN number,f IN OUT number) IS
2 BEGIN
3 IF(d>sysdate) THEN
4 f:=0;
5 ELSE
6 insert into TRANSACTION values(a,b,c,d,e);
7 f:=1;
8 END IF;
9 END;
10 /

Procedure created.
SQL>
SQL> DECLARE
2 Tid varchar2(5):='&TransactionId';
3 Ano varchar2(5):='&Ano';
4 Ttype varchar2(40):='&Ttype';
5 Tdate date:='&Tdate';
6 Tamount number:=&Tamount;
7 ex exception;
8 temp number;
9 BEGIN
10 temp:=0;
11 Trans(Tid,Ano,Ttype,Tdate,Tamount,temp);
12 IF temp=0 THEN
13 RAISE ex;
14 END IF;
15 EXCEPTION
16 WHEN ex THEN
17 dbms_output.put_line('Transaction date is more than
todays date !');
18 END;
19 /

Enter value for transactionid: T5


old 2: Tid varchar2(5):='&TransactionId';
new 2: Tid varchar2(5):='T5';
Enter value for ano: 100
old 3: Ano varchar2(5):='&Ano';
new 3: Ano varchar2(5):='100';
Enter value for ttype: D
old 4: Ttype varchar2(40):='&Ttype';
new 4: Ttype varchar2(40):='D';
Enter value for tdate: 06-10-2017
old 5: Tdate date:='&Tdate';
new 5: Tdate date:='06-10-2017';
Enter value for tamount: 100
old 6: Tamount number:=&Tamount;
new 6: Tamount number:=100;
Transaction date is more than todays date !

PL/SQL procedure successfully completed.

SQL>