home *** CD-ROM | disk | FTP | other *** search
/ Freelog 42 / Freelog042.iso / Alu / Ancestrologie / Sources / InterBase_WI-V6.0.1-server.ZIP / examples / gpre / stat5.e < prev    next >
Encoding:
Text File  |  2001-01-05  |  2.9 KB  |  117 lines

  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.  *
  23.  * Copyright (C) 2000 Inprise Corporation
  24.  * All Rights Reserved.
  25.  * Contributor(s): ______________________________________.
  26.  */
  27.  
  28. #include "example.h"
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31.  
  32. EXEC SQL
  33.     BEGIN DECLARE SECTION;
  34.  
  35. BASED_ON job.job_code        job;
  36. BASED_ON job.job_grade        grade;
  37. BASED_ON job.job_country    country;
  38. BASED_ON job.max_salary        max_salary;
  39.  
  40. EXEC SQL
  41.     END DECLARE SECTION;
  42.  
  43.  
  44. int main (void)
  45. {
  46.     char    jobstr[25];
  47.     float    mult_factor;
  48.  
  49.     EXEC SQL
  50.         WHENEVER SQLERROR GO TO Error;
  51.  
  52.     /* Declare the cursor, allowing for the update of max_salary field. */
  53.     EXEC SQL
  54.         DECLARE sal_range CURSOR FOR
  55.         SELECT job_grade, job_code, job_country, max_salary
  56.         FROM job
  57.         FOR UPDATE OF max_salary;
  58.  
  59.     EXEC SQL
  60.         OPEN sal_range;
  61.  
  62.     printf("\nIncreasing maximum salary limit for the following jobs:\n\n");
  63.     printf("%-25s%-22s%-22s\n\n", "  JOB NAME", "CURRENT MAX", "NEW MAX");
  64.  
  65.     for (;;)
  66.     {
  67.         EXEC SQL
  68.             FETCH sal_range INTO :grade, :job, :country, :max_salary;
  69.  
  70.         if (SQLCODE == 100)
  71.             break;
  72.  
  73.         /* Check if any of the employees in this job category are within
  74.          * 10% of the maximum salary.
  75.          */
  76.         EXEC SQL
  77.             SELECT salary
  78.             FROM employee
  79.             WHERE job_grade = :grade
  80.             AND job_code = :job
  81.             AND job_country = :country
  82.             AND salary * 0.1 + salary > :max_salary;
  83.  
  84.         /* If so, increase the maximum salary. */
  85.         if (SQLCODE == 0)
  86.         {
  87.             /* Determine the increase amount;  for example, 5%. */
  88.             mult_factor = 0.05;
  89.  
  90.             sprintf(jobstr, "%s %d  (%s)", job, grade, country);
  91.             printf("%-25s%10.2f%20.2f\n", jobstr,
  92.                         max_salary, max_salary * mult_factor + max_salary);
  93.  
  94.             EXEC SQL
  95.                 UPDATE job
  96.                 SET max_salary = :max_salary + :max_salary * :mult_factor
  97.                 WHERE CURRENT OF sal_range;
  98.         }
  99.     }
  100.  
  101.     printf("\n");
  102.  
  103.     EXEC SQL
  104.         CLOSE sal_range;
  105.  
  106.     /* Don't actually save the changes. */
  107.     EXEC SQL
  108.         ROLLBACK RELEASE;
  109.  
  110.     return  0;
  111.  
  112. Error:
  113.     isc_print_sqlerror(SQLCODE, gds__status);
  114.     return  1 ;
  115. }
  116.  
  117.