0% found this document useful (0 votes)
24 views

Assignment 7

The document contains solutions to two SQL procedures. The first procedure accepts a ticket number as input and displays the ticket header details if the total fare is not null, or displays an error message if it is null. The second procedure accepts a route ID as input and updates the capacity field in the route header table to 25 if the category code is 1, or to 50 if the category code is 2. Both procedures handle exceptions if the input data is not found.

Uploaded by

subhajitbasak001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Assignment 7

The document contains solutions to two SQL procedures. The first procedure accepts a ticket number as input and displays the ticket header details if the total fare is not null, or displays an error message if it is null. The second procedure accepts a route ID as input and updates the capacity field in the route header table to 25 if the category code is 1, or to 50 if the category code is 2. Both procedures handle exceptions if the input data is not found.

Uploaded by

subhajitbasak001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment 7

1. Write a procedure to accept the ticket_no as input and display an error message if the
total_fare is null otherwise display the ticket_header. [Use: ticket_header]

Solution:
SQL> CREATE OR REPLACE PROCEDURE DisplayTicketInfo(ticket_no_input IN
ticket_header.ticket_no%TYPE) IS
2 v_total_fare ticket_header.total_fare%TYPE;
3 BEGIN
4 -- Retrieve the total fare for the given ticket number
5 SELECT total_fare INTO v_total_fare
6 FROM ticket_header
7 WHERE ticket_no = ticket_no_input;
8
9 -- Check if the total fare is null
10 IF v_total_fare IS NULL THEN
11 DBMS_OUTPUT.PUT_LINE('Error: Total fare is NULL for the given ticket
number.');
12 ELSE
13 -- Display ticket header details
14 DBMS_OUTPUT.PUT_LINE('Ticket Header Details:');
15 DBMS_OUTPUT.PUT_LINE('------------------------------------------');
16 DBMS_OUTPUT.PUT_LINE('Fleet ID Ticket No Time Travel Board Place Origin
Destination Adults Children Total Fare Route ID DOT');
17
18 FOR ticket_rec IN (SELECT * FROM ticket_header WHERE
ticket_no = ticket_no_input) LOOP
19 DBMS_OUTPUT.PUT_LINE(ticket_rec.FLEET_ID || ' ' ||
ticket_rec.TICKET_NO || ' ' || ticket_rec.TIME_TRAVEL || '
' || ticket_rec.BOARD_PLACE || ' ' || ticket_rec.ORIGIN ||
' ' || ticket_rec.DESTINATION || ' ' ||
ticket_rec.ADULTS || ' ' || ticket_rec.CHILDREN || ' '
|| ticket_rec.TOTAL_FARE || ' ' || ticket_rec.ROUTE_ID ||
' ' || ' ' || ticket_rec.DOT);
20 END LOOP;
21 END IF;
22 EXCEPTION
23 WHEN NO_DATA_FOUND THEN
24 DBMS_OUTPUT.PUT_LINE('Ticket number ' || ticket_no_input || 'not found.');
25 WHEN OTHERS THEN
26 DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
27 END;
28 /

SQL> BEGIN
DisplayTicketInfo(5);
END;
/
Ticket Header Details:
-------------------------------------------------------------------------
Fleet ID Ticket No Time Travel Board Place Origin
5 5 25:00:00 PARRYS MADRAS
Destination Adults Children Total Fare Route ID DOT
COCHIN 2 2 141 103 22-MAY-1996
PL/SQL procedure successfully completed.

2. write a procedure to accept the route_id as input and update the capacity to be 25 if the
cat_code is 1 and 50 if the cat_code is 2. [Use: route_header]

Solution:
SQL> CREATE OR REPLACE PROCEDURE UpdateCapacity(p_route_id IN NUMBER) AS
2 v_cat_code route_header.cat_code%TYPE;
3 BEGIN
4 -- Fetching CAT_CODE for the given ROUTE_ID
5 SELECT CAT_CODE INTO v_cat_code
6 FROM ROUTE_HEADER
7 WHERE ROUTE_ID = p_route_id;
8
9 -- Updating CAPACITY based on CAT_CODE
10 IF v_cat_code = 1 THEN
11 UPDATE ROUTE_HEADER
12 SET CAPACITY = 25
13 WHERE ROUTE_ID = p_route_id;
14 ELSIF v_cat_code = 2 THEN
15 UPDATE ROUTE_HEADER
16 SET CAPACITY = 50
17 WHERE ROUTE_ID = p_route_id;
18 ELSE
19 DBMS_OUTPUT.PUT_LINE('Error: Unknown CAT_CODE for ROUTE_ID '||
p_route_id);
20 END IF;
21 EXCEPTION
22 WHEN NO_DATA_FOUND THEN
23 DBMS_OUTPUT.PUT_LINE('Error: ROUTE_ID ' || p_route_id || '
not found in ROUTE_HEADER.');
24 WHEN OTHERS THEN
25 DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
26 END UpdateCapacity;
27 /
SQL> select route_id, cat_code, capacity from Route_header;
ROUTE_ID CAT_CODE CAPACITY
---------------- ---------------- --------------
101 1 25
102 2 50
103 3 50
105 1 50
106 2 50
107 3 50
108 4 50
7 rows selected.
SQL> begin
2 UpdateCapacity(105);
3 end;
4 /

PL/SQL procedure successfully completed.


SQL> select route_id, cat_code, capacity from Route_header;
ROUTE_ID CAT_CODE CAPACITY
---------------- ----------------- ---------------
101 1 25
102 2 50
103 3 50
105 1 25
106 2 50
107 3 50
108 4 50
7 rows selected.

You might also like