stat4.e 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Program type: Embedded Static SQL
  3. *
  4. * Description:
  5. * This program declares and creates a new table.
  6. * Some rows, describing a department structure,
  7. * are added to the new table as a test.
  8. *
  9. * The table is created temporarily for figuring
  10. * out which level in the department structure each
  11. * department belongs too.
  12. * The contents of this file are subject to the Interbase Public
  13. * License Version 1.0 (the "License"); you may not use this file
  14. * except in compliance with the License. You may obtain a copy
  15. * of the License at http://www.Inprise.com/IPL.html
  16. *
  17. * Software distributed under the License is distributed on an
  18. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  19. * or implied. See the License for the specific language governing
  20. * rights and limitations under the License.
  21. *
  22. * The Original Code was created by Inprise Corporation
  23. * and its predecessors. Portions created by Inprise Corporation are
  24. * Copyright (C) Inprise Corporation.
  25. *
  26. * All Rights Reserved.
  27. * Contributor(s): ______________________________________.
  28. */
  29. #include "example.h"
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. void build_tree (void);
  33. void pr_error (void);
  34. EXEC SQL
  35. BEGIN DECLARE SECTION;
  36. EXEC SQL
  37. END DECLARE SECTION;
  38. int main (void)
  39. {
  40. char dept[4];
  41. int lvl = 1;
  42. /* Describe the new table's structure. */
  43. EXEC SQL
  44. DECLARE tmp_dept_tree TABLE (
  45. dept_no CHAR(3) NOT NULL PRIMARY KEY,
  46. tree_level INTEGER);
  47. /* Drop the table in case it exists. Ignore error */
  48. EXEC SQL
  49. DROP TABLE tmp_dept_tree;
  50. EXEC SQL
  51. WHENEVER SQLERROR GO TO Error1;
  52. /* Create the new table. */
  53. printf ("creating tmp_dept_tree\n");
  54. EXEC SQL
  55. CREATE TABLE tmp_dept_tree (
  56. dept_no CHAR(3) NOT NULL PRIMARY KEY,
  57. tree_level INTEGER);
  58. /* Fill the new table. */
  59. build_tree();
  60. /* Look at it */
  61. EXEC SQL DECLARE dc CURSOR FOR
  62. SELECT dept_no, tree_level
  63. FROM tmp_dept_tree;
  64. EXEC SQL
  65. OPEN dc;
  66. EXEC SQL
  67. FETCH dc INTO :dept, :lvl;
  68. while (SQLCODE == 0)
  69. {
  70. printf ("Dept = %s: Level = %d\n", dept, lvl);
  71. EXEC SQL
  72. FETCH dc INTO :dept, :lvl;
  73. }
  74. EXEC SQL
  75. COMMIT RELEASE;
  76. return 0;
  77. Error1:
  78. pr_error();
  79. return 1;
  80. }
  81. /*
  82. * For each department find its sub-departments and mark them
  83. * with the next level number.
  84. */
  85. void build_tree (void)
  86. {
  87. char dept[4];
  88. int lvl = 1;
  89. EXEC SQL
  90. WHENEVER SQLERROR GO TO Error2;
  91. /* Initialize the department structure by adding the first level. */
  92. EXEC SQL
  93. INSERT INTO tmp_dept_tree VALUES ('000', :lvl);
  94. /* Declare the cursor for selecting all departments with level 'lvl'. */
  95. EXEC SQL
  96. DECLARE ds CURSOR FOR
  97. SELECT dept_no
  98. FROM tmp_dept_tree
  99. WHERE tree_level = :lvl;
  100. /* For each department with level 'lvl', find its sub-departments. */
  101. while (SQLCODE == 0)
  102. {
  103. EXEC SQL
  104. OPEN ds;
  105. /* Initialize the next level. */
  106. lvl++;
  107. /* Add all the sub-departments of the next level to the table. */
  108. for (;;)
  109. {
  110. EXEC SQL
  111. FETCH ds INTO :dept;
  112. if (SQLCODE == 100)
  113. break;
  114. EXEC SQL
  115. INSERT INTO tmp_dept_tree
  116. SELECT dept_no, :lvl
  117. FROM department
  118. WHERE head_dept = :dept;
  119. }
  120. EXEC SQL
  121. CLOSE ds;
  122. EXEC SQL
  123. COMMIT;
  124. /* Done, if no next level was created by the INSERT above. */
  125. EXEC SQL
  126. SELECT tree_level
  127. FROM tmp_dept_tree
  128. WHERE tree_level = :lvl;
  129. if (SQLCODE == 100)
  130. return;
  131. }
  132. Error2:
  133. pr_error();
  134. return;
  135. }
  136. /*
  137. * Print any error and exit.
  138. */
  139. void pr_error (void)
  140. {
  141. isc_print_sqlerror((short)SQLCODE, gds__status);
  142. }