Advertisement
Guest User

Question 3 from books homework

a guest
Oct 22nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DECLARE
  2.     /*
  3.         Create new cursor.
  4.         We use 'r_date is null' to check for book state
  5.         r_date is null     = not returned yet
  6.         r_date is not null = returned already
  7.     */
  8.     CURSOR bk_c IS
  9.         SELECT * FROM borrow
  10.         WHERE r_date IS NULL;
  11.    
  12.     /* Declare a new record for our cursor */  
  13.     bk_rec bk_c%ROWTYPE;
  14.    
  15.     /* Assuming the current date is 10th of November */
  16.     /* You should use sysdate for real application */
  17.     crnt_date borrow.r_date%TYPE := TO_DATE('10-NOV-18');
  18.    
  19.     /* Declare member's name and mobile variables */
  20.     mem_name member.name%TYPE;
  21.     mem_mobile member.mobile%TYPE;
  22.    
  23.     days_since_borrowed NUMBER;
  24. BEGIN
  25.    
  26.     /* Open cursor and loop through result */
  27.     OPEN bk_c;
  28.     LOOP
  29.         /* Fetch result into our record */
  30.         FETCH bk_c INTO bk_rec;
  31.         /* Exit loop if no result found */
  32.         EXIT WHEN bk_c%notfound;
  33.        
  34.         /* Find out member name and mobile */
  35.         SELECT name, mobile INTO mem_name, mem_mobile
  36.         FROM member WHERE m_id = bk_rec.m_id;
  37.        
  38.         /*
  39.             Calculate how many days since the book borrowed
  40.             By subtracting the borrow date from current date
  41.             Use FLOOR to avoid decimals.
  42.         */
  43.         days_since_borrowed := FLOOR(crnt_date - bk_rec.b_date);
  44.        
  45.         /* Print result */
  46.         dbms_output.put_line(mem_name || ' ' || mem_mobile ||
  47.             ' has not returned his book since '
  48.             || days_since_borrowed || ' days.');
  49.            
  50.     END LOOP;
  51.    
  52.     CLOSE bk_c;
  53. END;
  54. /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement