RDBMS SOLUTION


ASSIGNMENT 5 

SET B Q. SOLUTION 2

Write a cursor which will display city wise Gym details.




Source code:

    

    /* set b q 2 solution

    here we will use order by clause

        explaination of the code at the end*/

        

Declare

    v_gname gym.gname%type;

    v_city gym.city%type;

    v_charges gym.charges%type;

    cursor gym_det is

    select gname,city,charges from gym

    order by city;

begin

dbms_output.put_line('Name      city        charges');

open gym_det;

loop

exit when gym_det%notfound;

fetch gym_det into v_gname,v_city,v_charges;

dbms_output.put_line(v_gname|| '    '||v_city||'  '||v_charges);

end loop;

close gym_det;

end;

/* Explaination:

This is a PL/SQL block of code used to retrieve and display data from a table named "gym". 

The first three lines declare three variables v_gname, v_city, and v_charges with their respective data types. These variables will be used to store the values retrieved from the "gym" table.

The next line defines a cursor named "gym_det" that selects the columns "gname", "city", and "charges" from the "gym" table and orders the results by the "city" column.

The "dbms_output.put_line" statement is used to display the column headers "Name", "city", and "charges" on the console.

The "open gym_det" statement opens the cursor and starts the loop to retrieve the data from the "gym" table.

The "loop" statement starts a loop that will execute until there are no more rows to fetch.

The "exit when gym_det%notfound" statement checks if there are any more rows to fetch. If there are no more rows, the loop is exited.

The "fetch gym_det into v_gname, v_city, v_charges" statement retrieves the next row from the "gym_det" cursor and stores the values in the variables.

The "dbms_output.put_line" statement prints the values of the variables v_gname, v_city, and v_charges on the console.

Overall, this code retrieves the data from the "gym" table and displays it on the console in a tabular format with headers.

*/

   

    
 Download code         next