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

  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.  *
  22.  * Copyright (C) 2000 Inprise Corporation
  23.  * All Rights Reserved.
  24.  * Contributor(s): ______________________________________.
  25.  */
  26.  
  27. #include "example.h"
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdio.h>
  31.  
  32. EXEC SQL INCLUDE SQLCA;
  33.  
  34. void select_projects (short emp_no);
  35. void get_params (short *emp_no, char* proj_id);
  36. void pr_error (long status);
  37. int add_emp_proj (short emp_no,char * proj_id);
  38.  
  39.  
  40.  
  41. int main (void)
  42. {
  43.     BASED_ON employee.emp_no    emp_no;
  44.     BASED_ON project.proj_id    proj_id;
  45.  
  46.     /*
  47.      *    Add employee with id 8 to project 'MAPDB'.
  48.      */
  49.     get_params(&emp_no, proj_id);
  50.  
  51.     /*
  52.      *    Display employee's current projects.
  53.      */
  54.     printf("\nCurrent projects for employee id: %d\n\n", emp_no);
  55.     select_projects(emp_no);
  56.  
  57.     /*
  58.      *    Insert a new employee project row.
  59.      */
  60.     printf("\nAdd employee id: %d to project: %s\n", emp_no, proj_id);
  61.     add_emp_proj(emp_no, proj_id);
  62.  
  63.     /*
  64.      *    Display employee's new current projects.
  65.      */
  66.     printf("\nCurrent projects for employee id: %d\n\n", emp_no);
  67.     select_projects(emp_no);
  68.  
  69. }
  70.  
  71.  
  72. /*
  73.  *    Select from a stored procedure.
  74.  *    Procedure 'get_emp_proj' gets employee's projects.
  75.  */
  76. void select_projects(ARG(short, emp_no))
  77.     ARGLIST(short emp_no)
  78. {
  79.     BASED_ON project.proj_id    proj_id;
  80.  
  81.     EXEC SQL
  82.         WHENEVER SQLERROR GO TO SelError;
  83.  
  84.     /* Declare a cursor on the stored procedure. */
  85.     EXEC SQL
  86.         DECLARE projects CURSOR FOR
  87.         SELECT proj_id FROM get_emp_proj (:emp_no)
  88.         ORDER BY proj_id;
  89.  
  90.     EXEC SQL
  91.         OPEN projects;
  92.     
  93.     /* Print employee projects. */
  94.     while (SQLCODE == 0)
  95.     {
  96.         EXEC SQL
  97.             FETCH projects INTO :proj_id;
  98.  
  99.         if (SQLCODE == 100)
  100.             break;
  101.  
  102.         printf("\t%s\n", proj_id);
  103.     }
  104.  
  105.     EXEC SQL
  106.         CLOSE projects;
  107.     
  108.     EXEC SQL
  109.         COMMIT RETAIN;
  110.  
  111. SelError:
  112.     if (SQLCODE)
  113.         pr_error((long)gds__status);
  114. }
  115.  
  116.  
  117. /*
  118.  *    Execute a stored procedure.
  119.  *    Procedure 'add_emp_proj' adds an employee to a project.
  120.  */
  121. add_emp_proj(ARG(short, emp_no),
  122.              ARG(char *, proj_id))
  123.     ARGLIST(BASED_ON employee.emp_no    emp_no)
  124.     ARGLIST(BASED_ON project.proj_id    proj_id)
  125. {
  126.     EXEC SQL
  127.         WHENEVER SQLERROR GO TO AddError;
  128.  
  129.     EXEC SQL
  130.         EXECUTE PROCEDURE add_emp_proj :emp_no, :proj_id;
  131.  
  132.     EXEC SQL
  133.         COMMIT;
  134.  
  135. AddError:
  136.     if (SQLCODE) 
  137.         pr_error((long)gds__status);
  138. }
  139.  
  140.  
  141. /*
  142.  *    Set-up procedure parameters and clean-up old data.
  143.  */
  144. void get_params(ARG(short *, emp_no),
  145.                 ARG(char *, proj_id))
  146. ARGLIST(BASED_ON employee.emp_no        *emp_no)
  147. ARGLIST(BASED_ON project.proj_id        proj_id)
  148. {
  149.     *emp_no = 8;
  150.     strcpy(proj_id, "MAPDB");
  151.  
  152.     EXEC SQL
  153.         WHENEVER SQLERROR GO TO CleanupError;
  154.  
  155.     /* Cleanup:  delete row from the previous run. */
  156.     EXEC SQL
  157.         DELETE FROM employee_project
  158.         WHERE emp_no = 8 AND proj_id = "MAPDB";
  159.  
  160. CleanupError:
  161.     return;
  162. }
  163.  
  164.  
  165. /*
  166.  *    Print an error message.
  167.  */
  168. void pr_error(ARG(long, status))
  169.     ARGLIST(long    status)
  170. {
  171.     isc_print_sqlerror(SQLCODE, gds__status);
  172. }
  173.  
  174.