stat5.e 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Program type: Embedded Static SQL
  3. *
  4. * Description:
  5. * This program performs a positioned update.
  6. * All job grades are selected, and the salary range
  7. * for the job may be increased by some factor, if any
  8. * of the employees have a salary close to the upper
  9. * limit of their job grade.
  10. * The contents of this file are subject to the Interbase Public
  11. * License Version 1.0 (the "License"); you may not use this file
  12. * except in compliance with the License. You may obtain a copy
  13. * of the License at http://www.Inprise.com/IPL.html
  14. *
  15. * Software distributed under the License is distributed on an
  16. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  17. * or implied. See the License for the specific language governing
  18. * rights and limitations under the License.
  19. *
  20. * The Original Code was created by Inprise Corporation
  21. * and its predecessors. Portions created by Inprise Corporation are
  22. * Copyright (C) Inprise Corporation.
  23. *
  24. * All Rights Reserved.
  25. * Contributor(s): ______________________________________.
  26. */
  27. #include "example.h"
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. EXEC SQL
  31. BEGIN DECLARE SECTION;
  32. BASED_ON job.job_code job;
  33. BASED_ON job.job_grade grade;
  34. BASED_ON job.job_country country;
  35. BASED_ON job.max_salary max_salary;
  36. EXEC SQL
  37. END DECLARE SECTION;
  38. int main (void)
  39. {
  40. char jobstr[25];
  41. float mult_factor;
  42. EXEC SQL
  43. WHENEVER SQLERROR GO TO Error;
  44. /* Declare the cursor, allowing for the update of max_salary field. */
  45. EXEC SQL
  46. DECLARE sal_range CURSOR FOR
  47. SELECT job_grade, job_code, job_country, max_salary
  48. FROM job
  49. FOR UPDATE OF max_salary;
  50. EXEC SQL
  51. OPEN sal_range;
  52. printf("\nIncreasing maximum salary limit for the following jobs:\n\n");
  53. printf("%-25s%-22s%-22s\n\n", " JOB NAME", "CURRENT MAX", "NEW MAX");
  54. for (;;)
  55. {
  56. EXEC SQL
  57. FETCH sal_range INTO :grade, :job, :country, :max_salary;
  58. if (SQLCODE == 100)
  59. break;
  60. /* Check if any of the employees in this job category are within
  61. * 10% of the maximum salary.
  62. */
  63. EXEC SQL
  64. SELECT salary
  65. FROM employee
  66. WHERE job_grade = :grade
  67. AND job_code = :job
  68. AND job_country = :country
  69. AND salary * 0.1 + salary > :max_salary;
  70. /* If so, increase the maximum salary. */
  71. if (SQLCODE == 0)
  72. {
  73. /* Determine the increase amount; for example, 5%. */
  74. mult_factor = 0.05;
  75. sprintf(jobstr, "%s %d (%s)", job, grade, country);
  76. printf("%-25s%10.2f%20.2f\n", jobstr,
  77. max_salary, max_salary * mult_factor + max_salary);
  78. EXEC SQL
  79. UPDATE job
  80. SET max_salary = :max_salary + :max_salary * :mult_factor
  81. WHERE CURRENT OF sal_range;
  82. }
  83. }
  84. printf("\n");
  85. EXEC SQL
  86. CLOSE sal_range;
  87. /* Don't actually save the changes. */
  88. EXEC SQL
  89. ROLLBACK RELEASE;
  90. return 0;
  91. Error:
  92. isc_print_sqlerror(SQLCODE, gds__status);
  93. return 1 ;
  94. }