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

  1. /*
  2.  *  Program type:   Embedded Static SQL
  3.  *
  4.  *  Description:
  5.  *        This program declares a cursor, opens the cursor, and loops
  6.  *        fetching multiple rows.  All departments that need to hire
  7.  *        a manager are selected and displayed.
  8.  * The contents of this file are subject to the Interbase Public
  9.  * License Version 1.0 (the "License"); you may not use this file
  10.  * except in compliance with the License. You may obtain a copy
  11.  * of the License at http://www.Inprise.com/IPL.html
  12.  *
  13.  * Software distributed under the License is distributed on an
  14.  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  15.  * or implied. See the License for the specific language governing
  16.  * rights and limitations under the License.
  17.  *
  18.  * The Original Code was created by Inprise Corporation
  19.  * and its predecessors. Portions created by Inprise Corporation are
  20.  *
  21.  * Copyright (C) 2000 Inprise Corporation
  22.  * All Rights Reserved.
  23.  * Contributor(s): ______________________________________.
  24.  */
  25.  
  26. #include "example.h"
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29.  
  30. EXEC SQL
  31.     BEGIN DECLARE SECTION;
  32. EXEC SQL
  33.     END DECLARE SECTION;
  34.  
  35.  
  36. int main (void)
  37. {
  38.     BASED_ON department.department    department;
  39.     BASED_ON department.department    parent_dept;
  40.     BASED_ON department.location    location;
  41.  
  42.     /* Trap all errors. */
  43.     EXEC SQL
  44.         WHENEVER SQLERROR GO TO Error;
  45.  
  46.     /* Trap SQLCODE = -100 (end of file reached during a fetch). */
  47.     EXEC SQL
  48.         WHENEVER NOT FOUND GO TO AllDone;
  49.  
  50.     /* Ignore all warnings. */
  51.     EXEC SQL
  52.         WHENEVER SQLWARNING CONTINUE;
  53.  
  54.     /* Declare the cursor for selecting all departments without a manager. */
  55.     EXEC SQL
  56.         DECLARE to_be_hired CURSOR FOR
  57.         SELECT d.department, d.location, p.department
  58.         FROM department d, department p
  59.         WHERE d.mngr_no IS NULL
  60.         AND d.head_dept = p.dept_no;
  61.  
  62.     /* Open the cursor. */
  63.     EXEC SQL
  64.         OPEN to_be_hired;
  65.  
  66.     printf("\n%-25s %-15s %-25s\n\n",
  67.                 "DEPARTMENT", "LOCATION", "HEAD DEPARTMENT");
  68.  
  69.     /*
  70.      *    Select and display all rows.
  71.      */
  72.     while (SQLCODE == 0)
  73.     {
  74.         EXEC SQL
  75.             FETCH to_be_hired INTO :department, :location, :parent_dept;
  76.  
  77.         /* 
  78.          *  If FETCH returns with -100, the processing will jump
  79.          *  to AllDone before the following printf is executed.
  80.          */
  81.         
  82.         printf("%-25s %-15s %-25s\n", department, location, parent_dept);
  83.     }
  84.  
  85.     /*
  86.      *    Close the cursor and release all resources.
  87.      */
  88. AllDone:
  89.     EXEC SQL
  90.         CLOSE to_be_hired;
  91.  
  92.     EXEC SQL
  93.         COMMIT RELEASE;
  94.  
  95. return 0;
  96.  
  97.     /*
  98.      *    Print the error, and exit.
  99.      */
  100. Error:
  101.     isc_print_sqlerror((short)SQLCODE, gds__status);
  102. return 1;
  103. }
  104.  
  105.