dyn2.e 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Program type: Embedded Dynamic SQL
  3. *
  4. * Description:
  5. * This program adds several departments with small default
  6. * budgets, using 'execute immediate' statement.
  7. * Then, a prepared statement, which doubles budgets for
  8. * departments with low budgets, is executed.
  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. #define MAXLEN 256
  30. int get_line (char *line);
  31. void clean_up (void);
  32. static char *Dept_data[] =
  33. {
  34. "117", "Field Office: Hong Kong", "110",
  35. "118", "Field Office: Australia", "110",
  36. "119", "Field Office: New Zealand", "110",
  37. 0
  38. };
  39. int Dept_ptr = 0;
  40. char Db_name[128];
  41. EXEC SQL
  42. SET DATABASE empdb = "employee.fdb" RUNTIME :Db_name;
  43. int main(int argc, char** argv)
  44. {
  45. BASED_ON department.department dept_name;
  46. BASED_ON department.dept_no dept_id;
  47. char exec_str[MAXLEN], prep_str[MAXLEN];
  48. if (argc > 1)
  49. strcpy(Db_name, argv[1]);
  50. else
  51. strcpy(Db_name, "employee.fdb");
  52. EXEC SQL
  53. WHENEVER SQLERROR GO TO MainError;
  54. EXEC SQL
  55. CONNECT empdb;
  56. clean_up();
  57. EXEC SQL
  58. SET TRANSACTION;
  59. /*
  60. * Prepare a statement, which may be executed more than once.
  61. */
  62. strcpy(prep_str,
  63. "UPDATE DEPARTMENT SET budget = budget * 2 WHERE budget < 100000");
  64. EXEC SQL
  65. PREPARE double_small_budget FROM :prep_str;
  66. /*
  67. * Add new departments, using 'execute immediate'.
  68. * Build each 'insert' statement, using the supplied parameters.
  69. * Since these statements will not be needed after they are executed,
  70. * use 'execute immediate'.
  71. */
  72. while (get_line(exec_str))
  73. {
  74. printf("\nExecuting statement:\n\t%s;\n", exec_str);
  75. EXEC SQL
  76. EXECUTE IMMEDIATE :exec_str;
  77. }
  78. EXEC SQL
  79. COMMIT RETAIN;
  80. /*
  81. * Execute a previously prepared statement.
  82. */
  83. printf("\nExecuting a prepared statement:\n\t%s;\n\n", prep_str);
  84. EXEC SQL
  85. EXECUTE double_small_budget;
  86. EXEC SQL
  87. COMMIT;
  88. EXEC SQL
  89. DISCONNECT empdb;
  90. exit(0);
  91. MainError:
  92. EXEC SQL
  93. WHENEVER SQLERROR CONTINUE;
  94. isc_print_status(gds__status);
  95. printf("SQLCODE=%d\n", SQLCODE);
  96. EXEC SQL ROLLBACK;
  97. exit(1);
  98. }
  99. /*
  100. * Construct an 'insert' statement from the supplied parameters.
  101. */
  102. int get_line(char* line)
  103. {
  104. if (Dept_data[Dept_ptr] == 0)
  105. return 0;
  106. if (Dept_data[Dept_ptr + 1] == 0)
  107. return 0;
  108. if (Dept_data[Dept_ptr + 2] == 0)
  109. return 0;
  110. sprintf(line, "INSERT INTO DEPARTMENT (dept_no, department, head_dept) \
  111. VALUES ('%s', '%s', '%s')", Dept_data[Dept_ptr],
  112. Dept_data[Dept_ptr + 1], Dept_data[Dept_ptr + 2]);
  113. Dept_ptr += 3;
  114. return(1);
  115. }
  116. /*
  117. * Delete old data.
  118. */
  119. void clean_up (void)
  120. {
  121. EXEC SQL WHENEVER SQLERROR GO TO CleanErr;
  122. EXEC SQL SET TRANSACTION;
  123. EXEC SQL EXECUTE IMMEDIATE
  124. "DELETE FROM department WHERE dept_no IN ('117', '118', '119')";
  125. EXEC SQL COMMIT;
  126. return;
  127. CleanErr:
  128. isc_print_status(gds__status);
  129. printf("SQLCODE=%d\n", SQLCODE);
  130. exit(1);
  131. }