Update DBMS/SQL/Week6/writeup.md

This commit is contained in:
Aadit Agrawal 2025-02-11 16:15:52 +05:30
parent 8177218e4e
commit a2d50a1f13

View File

@ -26,24 +26,23 @@ TOTAL_ACCD(2024)
### 2. Create a procedure to display total damage caused due to an accident for a particular driver on a specific year.
```SQL
create or replace procedure tot_damage
(driver in varchar2, year in number) is
damage number := 0;
begin
select sum(damage_amount) into damage
from participated pa
natural join accident ac
where pa."driver_id#" = driver
with quotes
and extract(year from ac.accd_date) = year;
CREATE OR REPLACE PROCEDURE tot_damage
(driver IN VARCHAR2, year IN NUMBER) IS
damage NUMBER := 0;
BEGIN
SELECT NVL(SUM(pa.DAMAGE_AMOUNT), 0) INTO damage
FROM participated pa
JOIN accident ac ON pa.REPORT_NUMBER = ac.REPORT_NUMBER
WHERE pa.DRIVER_ID# = driver
AND EXTRACT(YEAR FROM ac.ACCD_DATE) = year;
dbms_output.put_line('total damage: ' || nvl(damage, 0));
exception
when no_data_found then
dbms_output.put_line('no data found for the given driver and year.');
when others then
dbms_output.put_line('an error occurred: ' || sqlerrm);
end;
DBMS_OUTPUT.PUT_LINE('Total damage: ' || damage);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for the given driver and year.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
/
```
```sql