home *** CD-ROM | disk | FTP | other *** search
/ Datatid 1999 #6 / Datatid_1999-06.iso / internet / Tango352Promo / P.SQL / PTKPKG.1 / UNIVDEMO.SQL < prev    next >
Encoding:
Text File  |  1996-06-06  |  6.3 KB  |  172 lines

  1. SET BINARYNULL = 255#
  2.  
  3. -------------------------------------------------------------------------
  4. -- The following Scalable SQL statements create tables, procedures, and
  5. -- triggers that demonstate how these constructs can be used to check the
  6. -- maximum enrollment limit of a college class before allowing a student
  7. -- to enroll.
  8. -------------------------------------------------------------------------
  9.  
  10. -- Create the table for the General Studies class.  In this simplified
  11. -- example, this table just consists of the identification numbers of
  12. -- the students that are enrolled in the class.
  13. --
  14. CREATE TABLE GeneralStudies
  15.   (StudentId INTEGER(4))#
  16.  
  17. -- Create the table that defines the maximum enrollment allowed for each
  18. -- of the college classes.  The ClassId column is an identification
  19. -- number associated with each of the college classes.  (In our example,
  20. -- the General Studies class will have an identification number of 1.)
  21. --
  22. CREATE TABLE ClassMax
  23.   (ClassId INTEGER(4), MaxSize INTEGER(4))#
  24.  
  25. -- Create the table that defines the current number of students enrolled
  26. -- in each of the college classes.  The ClassId column is an
  27. -- identification number associated with each of the college classes.
  28. -- (In our example, the General Studies class will have an identification
  29. -- number of 1.)
  30. --
  31. CREATE TABLE ClassActual
  32.   (ClassId INTEGER(4), ActualSize INTEGER(4))#
  33.  
  34. -- Create the external procedure that will be called to generate a
  35. -- rejection letter or an acceptance letter for a student who is
  36. -- attempting to enroll in a college class.  This external procedure
  37. -- declaration must have the same name and parameter types as the
  38. -- Letter() procedure defined in the UNIVDEMO.SBL script.
  39. --
  40. CREATE PROCEDURE Letter(IN StudentId  INTEGER(4),
  41.                         IN ClassName  CHAR(60),
  42.                         IN Confirm    CHAR(1));
  43.   EXTERNAL#
  44.  
  45. -- Create the stored procedure that checks the current enrollment of a
  46. -- college class against the maximum allowed enrollment.  If the class
  47. -- is already at its maximum enrollment, a FailEnrollment condition is
  48. -- signaled and a SBL script is invoked to generate a rejection letter.
  49. -- If the class is not at its maximum enrollment, an AcceptEnrollment
  50. -- condition is signaled and a SBL script is invoked to generate an
  51. -- acceptance letter. 
  52. --
  53. -- This procedure will be invoked by a trigger defined for insert
  54. -- operations on the General Studies table so that class enrollment can
  55. -- be checked to determine whether the insert operation should succeed.
  56. --
  57. CREATE PROCEDURE CheckEnrollment(IN student_id INTEGER(4),
  58.                                  IN class_id   INTEGER(4),
  59.                                  IN class_name CHAR(30));
  60.   BEGIN
  61.     DECLARE NumEnrolled INTEGER(4);
  62.     DECLARE MaxEnrollment INTEGER(4);
  63.  
  64.     DECLARE FailEnrollment CONDITION
  65.       FOR SQLSTATE '09000';
  66.  
  67.     -- Get the number of students currently enrolled for this class.
  68.     --
  69.     SET NumEnrolled = (SELECT ActualSize
  70.                          FROM ClassActual WHERE ClassId = class_id);
  71.  
  72.     -- Get the maximum number of students allowed for this class.
  73.     --
  74.     SET MaxEnrollment = (SELECT MaxSize
  75.                            FROM ClassMax WHERE ClassId = class_id);
  76.  
  77.     -- Check whether the current enrollment is less than the maximum
  78.     -- allowed enrollment.
  79.     --
  80.     IF (NumEnrolled >= MaxEnrollment) THEN
  81.       -- Call the SBL script Letter() to generate a rejection letter.
  82.       --
  83.       Call Letter(student_id, class_name, 'N');
  84.       SIGNAL FailEnrollment;
  85.  
  86.     ELSE
  87.       -- Call the SBL script Letter() to generate an acceptance letter.
  88.       -- Also increment the number of students enrolled in the class.
  89.       --
  90.       Call Letter(student_id, class_name, 'Y');
  91.       UPDATE ClassActual
  92.         SET ActualSize = (ActualSize + 1)
  93.         WHERE ClassId = class_id;
  94.     END IF;
  95.   END#
  96.  
  97. -- Create a 'Before Insert' trigger on the GeneralStudies table.  This
  98. -- trigger invokes the CheckEnrollment stored procedure to ensure that
  99. -- this course is not already at its maximum enrollment.  In this
  100. -- example, the GeneralStudies class has a class identification number
  101. -- of 1.
  102. --
  103. CREATE TRIGGER CheckGeneralStudiesLimit
  104.   BEFORE INSERT
  105.   ON GeneralStudies
  106.   REFERENCING NEW AS N
  107.   FOR EACH ROW
  108.   BEGIN
  109.     DECLARE ClassName CHAR(30) = 'General Studies';
  110.     DECLARE Id INTEGER(4) = 1;
  111.  
  112.     CALL CheckEnrollment(N.StudentId, Id, ClassName);
  113.   END#
  114.  
  115.  
  116. ------------------------------------------------------------------------
  117. -- The following Scalable SQL statements initialize the ClassMax and
  118. -- ClassActual tables and then insert a series of values into the
  119. -- GeneralStudies table to enroll a group of students in the General
  120. -- Studies class.  These enrollments will succeed and acceptance letters
  121. -- will be generated by the Letter() procedure in UNIVDEMO.SBL script as
  122. -- long as the maximum enrollment has not been reached.  When the
  123. -- maximum enrollment is reached, however, subsequent enrollments will
  124. -- fail and rejection letters will be generated by Letter().
  125. ------------------------------------------------------------------------
  126.  
  127. -- Initialize the ClassMax and ClassActual tables for this example.  The
  128. -- class maximum for the General Studies class (with class identification
  129. -- number 1) is set to 3 and the actual enrollment is initialized to 0.
  130. --
  131. INSERT INTO ClassMax
  132.   VALUES(1, 3)#
  133.  
  134. INSERT INTO ClassActual
  135.   VALUES(1, 0)#
  136.  
  137. -- Attempt to enroll 5 students in the General Studies class.  The first
  138. -- 3 enrollments for Anthony Happy (student id 777777777), August Hartig
  139. -- (student id 888888888), and John Jackson (student id 123456789) should
  140. -- succeed, but the last 2 enrollments for Jane Doe (student id
  141. -- 234567890) and John Smith (student id 345678901) should fail.
  142. --
  143. INSERT INTO GeneralStudies
  144.   VALUES(777777777)#
  145.  
  146. INSERT INTO GeneralStudies
  147.   VALUES(888888888)#
  148.  
  149. INSERT INTO GeneralStudies
  150.   VALUES(123456789)#
  151.  
  152. INSERT INTO GeneralStudies
  153.   VALUES(234567890)#
  154.  
  155. INSERT INTO GeneralStudies
  156.   VALUES(345678901)#
  157.  
  158. -- Clean up the database by dropping the tables, procedures, and trigger
  159. -- defined above.
  160. --
  161. DROP TRIGGER CheckGeneralStudiesLimit#
  162.  
  163. DROP PROCEDURE CheckEnrollment#
  164.  
  165. DROP PROCEDURE Letter#
  166.  
  167. DROP TABLE ClassActual#
  168.  
  169. DROP TABLE ClassMax#
  170.  
  171. DROP TABLE GeneralStudies#
  172.