stat9.e 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Program type: Embedded Static SQL
  3. *
  4. * Description:
  5. * This program executes a stored procedure and selects from
  6. * a stored procedure. First, a list of projects an employee
  7. * is involved in is printed. Then the employee is added to
  8. * another project. The new list of projects is printed again.
  9. * The contents of this file are subject to the Interbase Public
  10. * License Version 1.0 (the "License"); you may not use this file
  11. * except in compliance with the License. You may obtain a copy
  12. * of the License at http://www.Inprise.com/IPL.html
  13. *
  14. * Software distributed under the License is distributed on an
  15. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  16. * or implied. See the License for the specific language governing
  17. * rights and limitations under the License.
  18. *
  19. * The Original Code was created by Inprise Corporation
  20. * and its predecessors. Portions created by Inprise Corporation are
  21. * Copyright (C) Inprise Corporation.
  22. *
  23. * All Rights Reserved.
  24. * Contributor(s): ______________________________________.
  25. */
  26. #include "example.h"
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <stdio.h>
  30. EXEC SQL INCLUDE SQLCA;
  31. void select_projects (short emp_no);
  32. void get_params (short *emp_no, char* proj_id);
  33. void pr_error (long status);
  34. void add_emp_proj (short emp_no,char * proj_id);
  35. int main (void)
  36. {
  37. BASED_ON employee.emp_no emp_no;
  38. BASED_ON project.proj_id proj_id;
  39. /*
  40. * Add employee with id 8 to project 'MAPDB'.
  41. */
  42. get_params(&emp_no, proj_id);
  43. /*
  44. * Display employee's current projects.
  45. */
  46. printf("\nCurrent projects for employee id: %d\n\n", emp_no);
  47. select_projects(emp_no);
  48. /*
  49. * Insert a new employee project row.
  50. */
  51. printf("\nAdd employee id: %d to project: %s\n", emp_no, proj_id);
  52. add_emp_proj(emp_no, proj_id);
  53. /*
  54. * Display employee's new current projects.
  55. */
  56. printf("\nCurrent projects for employee id: %d\n\n", emp_no);
  57. select_projects(emp_no);
  58. }
  59. /*
  60. * Select from a stored procedure.
  61. * Procedure 'get_emp_proj' gets employee's projects.
  62. */
  63. void select_projects(BASED_ON employee.emp_no emp_no)
  64. {
  65. BASED_ON project.proj_id proj_id;
  66. EXEC SQL
  67. WHENEVER SQLERROR GO TO SelError;
  68. /* Declare a cursor on the stored procedure. */
  69. EXEC SQL
  70. DECLARE projects CURSOR FOR
  71. SELECT proj_id FROM get_emp_proj (:emp_no)
  72. ORDER BY proj_id;
  73. EXEC SQL
  74. OPEN projects;
  75. /* Print employee projects. */
  76. while (SQLCODE == 0)
  77. {
  78. EXEC SQL
  79. FETCH projects INTO :proj_id;
  80. if (SQLCODE == 100)
  81. break;
  82. printf("\t%s\n", proj_id);
  83. }
  84. EXEC SQL
  85. CLOSE projects;
  86. EXEC SQL
  87. COMMIT RETAIN;
  88. SelError:
  89. if (SQLCODE)
  90. pr_error((long)gds__status);
  91. }
  92. /*
  93. * Execute a stored procedure.
  94. * Procedure 'add_emp_proj' adds an employee to a project.
  95. */
  96. void add_emp_proj(BASED_ON employee.emp_no emp_no, BASED_ON project.proj_id proj_id)
  97. {
  98. EXEC SQL
  99. WHENEVER SQLERROR GO TO AddError;
  100. EXEC SQL
  101. EXECUTE PROCEDURE add_emp_proj :emp_no, :proj_id;
  102. EXEC SQL
  103. COMMIT;
  104. AddError:
  105. if (SQLCODE)
  106. pr_error((long)gds__status);
  107. }
  108. /*
  109. * Set-up procedure parameters and clean-up old data.
  110. */
  111. void get_params(BASED_ON employee.emp_no *emp_no, BASED_ON project.proj_id proj_id)
  112. {
  113. *emp_no = 8;
  114. strcpy(proj_id, "MAPDB");
  115. EXEC SQL
  116. WHENEVER SQLERROR GO TO CleanupError;
  117. /* Cleanup: delete row from the previous run. */
  118. EXEC SQL
  119. DELETE FROM employee_project
  120. WHERE emp_no = 8 AND proj_id = "MAPDB";
  121. CleanupError:
  122. return;
  123. }
  124. /*
  125. * Print an error message.
  126. */
  127. void pr_error(long status)
  128. {
  129. isc_print_sqlerror(SQLCODE, gds__status);
  130. }