Advertisement
Guest User

Untitled

a guest
Apr 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 3.76 KB | None | 0 0
  1. SET SERVEROUTPUT ON;/
  2.  --ÎÒÊËÞ×ÈÒÜ SERVEROUTPUT
  3. DECLARE  
  4.     TYPE performance_type IS RECORD  
  5.         (person_code      PERSON.person_code%TYPE,  
  6.          person_name      PERSON.last_name%TYPE,  
  7.          current_sales    NUMBER(8,2),  
  8.          perform_percent  NUMBER(8,1),  
  9.          status           VARCHAR2(30)  
  10.         );  
  11.    
  12.     one_perform performance_type;  
  13.    
  14.     CURSOR person_cur IS  
  15.         SELECT *  
  16.         FROM   PERSON;  
  17.    
  18.     /* This procedure computes the performance and current total sales by    
  19.        one salesperson. The information for the salesperson is passed in  
  20.        as a record named a_person. If there are no sales for the day by the      
  21.        person then current_sales is set to zero. If the person has no    
  22.        history, for example, the person just joined today, then the    
  23.        perform_percent is set to zero.  
  24.      */  
  25.     PROCEDURE current_performance  
  26.         (a_person PERSON%ROWTYPE,  
  27.          a_perform OUT performance_type)  
  28.     IS  
  29.         CURSOR history_cur (person VARCHAR2) IS  
  30.             SELECT   AVG(tab2.product_price * tab1.quantity) avg_order  
  31.             FROM     PURCHASE_ARCHIVE tab1,  
  32.                      PRODUCT tab2  
  33.             WHERE    tab1.product_name = tab2.product_name  
  34.             GROUP BY tab1.salesperson  
  35.             HAVING   tab1.salesperson = person;  
  36.    
  37.         hist_rec history_cur%ROWTYPE;  
  38.         current_avg_sales NUMBER(8,2) := 0;  
  39.    
  40.     BEGIN  
  41.         a_perform.person_code := a_person.person_code;  
  42.         a_perform.person_name := a_person.last_name;  
  43.         a_perform.status := NULL;  
  44.    
  45.         BEGIN  
  46.             SELECT   SUM(tbl2.product_price * tbl1.quantity),  
  47.                      AVG(tbl2.product_price * tbl1.quantity)  
  48.             INTO     a_perform.current_sales,  
  49.                      current_avg_sales  
  50.             FROM     PURCHASE tbl1,  
  51.                      PRODUCT tbl2  
  52.             WHERE    tbl1.product_name = tbl2.product_name  
  53.             GROUP BY tbl1.salesperson  
  54.             HAVING   tbl1.salesperson = a_person.person_code;  
  55.         EXCEPTION  
  56.             WHEN NO_DATA_FOUND  
  57.             THEN  
  58.                 a_perform.status := 'Current purchases exception';  
  59.                 a_perform.current_sales := 0;  
  60.         END;  
  61.    
  62.         OPEN history_cur (a_person.person_code);  
  63.         FETCH history_cur INTO hist_rec;  
  64.         IF (history_cur%NOTFOUND)  
  65.         THEN  
  66.             a_perform.perform_percent := 0;  
  67.             IF (a_perform.status IS NULL)  
  68.             THEN  
  69.                 a_perform.status := 'Erroneous or no history';  
  70.             END IF;  
  71.         ELSE  
  72.             a_perform.perform_percent :=  
  73.                  100 * (current_avg_sales - hist_rec.avg_order)/  
  74.                                                   hist_rec.avg_order;  
  75.             a_perform.status := 'All fine';  
  76.         END IF;  
  77.         CLOSE history_cur;  
  78.     EXCEPTION  
  79.         WHEN NO_DATA_FOUND  
  80.         THEN  
  81.             a_perform.status := 'Exceptions found';  
  82.     END current_performance;  
  83.    
  84. BEGIN  
  85.     FOR person_rec IN person_cur  
  86.     LOOP  
  87.         current_performance(person_rec, one_perform);  
  88.    
  89.         DBMS_OUTPUT.put_line(one_perform.person_code ||  
  90.                              ' ' ||  
  91.                              one_perform.person_name ||  
  92.                              ' ' ||  
  93.                              one_perform.current_sales ||  
  94.                              ' ' ||  
  95.                              one_perform.perform_percent ||  
  96.                              ' ' ||  
  97.                              one_perform.status);  
  98.     END LOOP;  
  99. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement