stat3.e 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Program type: Embedded Static SQL
  3. *
  4. * Description:
  5. * This program declares a cursor, opens the cursor, and loops
  6. * fetching multiple rows. All departments that need to hire
  7. * a manager are selected and displayed.
  8. * The contents of this file are subject to the Interbase Public
  9. * License Version 1.0 (the "License"); you may not use this file
  10. * except in compliance with the License. You may obtain a copy
  11. * of the License at http://www.Inprise.com/IPL.html
  12. *
  13. * Software distributed under the License is distributed on an
  14. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  15. * or implied. See the License for the specific language governing
  16. * rights and limitations under the License.
  17. *
  18. * The Original Code was created by Inprise Corporation
  19. * and its predecessors. Portions created by Inprise Corporation are
  20. * Copyright (C) Inprise Corporation.
  21. *
  22. * All Rights Reserved.
  23. * Contributor(s): ______________________________________.
  24. */
  25. #include "example.h"
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. EXEC SQL
  29. BEGIN DECLARE SECTION;
  30. EXEC SQL
  31. END DECLARE SECTION;
  32. int main (void)
  33. {
  34. BASED_ON department.department department;
  35. BASED_ON department.department parent_dept;
  36. BASED_ON department.location location;
  37. /* Trap all errors. */
  38. EXEC SQL
  39. WHENEVER SQLERROR GO TO Error;
  40. /* Trap SQLCODE = -100 (end of file reached during a fetch). */
  41. EXEC SQL
  42. WHENEVER NOT FOUND GO TO AllDone;
  43. /* Ignore all warnings. */
  44. EXEC SQL
  45. WHENEVER SQLWARNING CONTINUE;
  46. /* Declare the cursor for selecting all departments without a manager. */
  47. EXEC SQL
  48. DECLARE to_be_hired CURSOR FOR
  49. SELECT d.department, d.location, p.department
  50. FROM department d, department p
  51. WHERE d.mngr_no IS NULL
  52. AND d.head_dept = p.dept_no;
  53. /* Open the cursor. */
  54. EXEC SQL
  55. OPEN to_be_hired;
  56. printf("\n%-25s %-15s %-25s\n\n",
  57. "DEPARTMENT", "LOCATION", "HEAD DEPARTMENT");
  58. /*
  59. * Select and display all rows.
  60. */
  61. while (SQLCODE == 0)
  62. {
  63. EXEC SQL
  64. FETCH to_be_hired INTO :department, :location, :parent_dept;
  65. /*
  66. * If FETCH returns with -100, the processing will jump
  67. * to AllDone before the following printf is executed.
  68. */
  69. printf("%-25s %-15s %-25s\n", department, location, parent_dept);
  70. }
  71. /*
  72. * Close the cursor and release all resources.
  73. */
  74. AllDone:
  75. EXEC SQL
  76. CLOSE to_be_hired;
  77. EXEC SQL
  78. COMMIT RELEASE;
  79. return 0;
  80. /*
  81. * Print the error, and exit.
  82. */
  83. Error:
  84. isc_print_sqlerror((short)SQLCODE, gds__status);
  85. return 1;
  86. }