Update DBMS/SQL/Week6/writeup.md
This commit is contained in:
		
							parent
							
								
									78565b88cb
								
							
						
					
					
						commit
						3f57583a7b
					
				
					 1 changed files with 43 additions and 13 deletions
				
			
		| 
						 | 
				
			
			@ -76,20 +76,20 @@ PL/SQL procedure successfully completed.
 | 
			
		|||
 | 
			
		||||
```SQL
 | 
			
		||||
SQL> CREATE OR REPLACE PROCEDURE GetDriversWithoutAccidents AS
 | 
			
		||||
  2  BEGIN
 | 
			
		||||
  3      FOR rec IN (
 | 
			
		||||
  4          SELECT p.DRIVER_ID#, p.NAME, p.ADDRESS
 | 
			
		||||
  5          FROM person p
 | 
			
		||||
  6          LEFT JOIN participated pa ON p.DRIVER_ID# = pa.DRIVER_ID#
 | 
			
		||||
  7          WHERE pa.REPORT_NUMBER IS NULL
 | 
			
		||||
  8      ) LOOP
 | 
			
		||||
  9          DBMS_OUTPUT.PUT_LINE('Driver ID: ' || rec.DRIVER_ID# ||
 | 
			
		||||
    begin
 | 
			
		||||
        for rec in (
 | 
			
		||||
            select p.driver_id#, p.name, p.address
 | 
			
		||||
            from person p
 | 
			
		||||
            left join participated pa on p.driver_id# = pa.driver_id#
 | 
			
		||||
            where pa.report_number is null
 | 
			
		||||
        ) loop
 | 
			
		||||
            dbms_output.put_line('driver id: ' || rec.driver_id# ||
 | 
			
		||||
 | 
			
		||||
 10                               ', Name: ' || rec.NAME ||
 | 
			
		||||
 11                               ', Address: ' || rec.ADDRESS);
 | 
			
		||||
 12      END LOOP;
 | 
			
		||||
 13  END;
 | 
			
		||||
 14  /
 | 
			
		||||
                                 ', name: ' || rec.name ||
 | 
			
		||||
                                 ', address: ' || rec.address);
 | 
			
		||||
        end loop;
 | 
			
		||||
    end;
 | 
			
		||||
    /
 | 
			
		||||
 | 
			
		||||
Procedure created.
 | 
			
		||||
```
 | 
			
		||||
| 
						 | 
				
			
			@ -101,3 +101,33 @@ Driver ID: 1235, Name: Rohit, Address: Banglalore India
 | 
			
		|||
PL/SQL procedure successfully completed.
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
Q5. Write a function that takes a REGNO as input and returns the total number of accidents in which the car was involved
 | 
			
		||||
```sql
 | 
			
		||||
SQL> create or replace function gettotalaccidentsbyregno(p_regno in varchar2)
 | 
			
		||||
    return number
 | 
			
		||||
    is
 | 
			
		||||
        v_total_accidents number;
 | 
			
		||||
    begin
 | 
			
		||||
        select count(*)
 | 
			
		||||
        into v_total_accidents
 | 
			
		||||
        from participated
 | 
			
		||||
        where regno = p_regno;
 | 
			
		||||
        return v_total_accidents;
 | 
			
		||||
    exception
 | 
			
		||||
        when no_data_found then
 | 
			
		||||
            return 0;
 | 
			
		||||
        when others then
 | 
			
		||||
            raise;
 | 
			
		||||
    end;
 | 
			
		||||
    /
 | 
			
		||||
 | 
			
		||||
Function created.
 | 
			
		||||
```
 | 
			
		||||
```SQL
 | 
			
		||||
SQL> SELECT GetTotalAccidentsByRegno('ABCD0001') AS Total_Accidents
 | 
			
		||||
  2  FROM dual;
 | 
			
		||||
 | 
			
		||||
TOTAL_ACCIDENTS
 | 
			
		||||
---------------
 | 
			
		||||
              1
 | 
			
		||||
```
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue