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