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 / stat3.e < prev    next >
Text File  |  2000-06-23  |  3KB  |  108 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.Interbase.com/IPL/
  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 Interbase Software Corporation
  19.  * and its successors. Portions created by Borland/Inprise are
  20.  * Copyright (C) 1992-1998 and 1999-2000 Borland/Inprise. Portions
  21.  * created by InterBase Software Corporation are Copyright (C)
  22.  * 1998-1999 InterBase Software Corporation.
  23.  *
  24.  * Copyright (C) 2000 InterBase Software Corporation
  25.  * All Rights Reserved.
  26.  * Contributor(s): ______________________________________.
  27.  */
  28.  
  29. #include "example.h"
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32.  
  33. EXEC SQL
  34.     BEGIN DECLARE SECTION;
  35. EXEC SQL
  36.     END DECLARE SECTION;
  37.  
  38.  
  39. int main (void)
  40. {
  41.     BASED_ON department.department    department;
  42.     BASED_ON department.department    parent_dept;
  43.     BASED_ON department.location    location;
  44.  
  45.     /* Trap all errors. */
  46.     EXEC SQL
  47.         WHENEVER SQLERROR GO TO Error;
  48.  
  49.     /* Trap SQLCODE = -100 (end of file reached during a fetch). */
  50.     EXEC SQL
  51.         WHENEVER NOT FOUND GO TO AllDone;
  52.  
  53.     /* Ignore all warnings. */
  54.     EXEC SQL
  55.         WHENEVER SQLWARNING CONTINUE;
  56.  
  57.     /* Declare the cursor for selecting all departments without a manager. */
  58.     EXEC SQL
  59.         DECLARE to_be_hired CURSOR FOR
  60.         SELECT d.department, d.location, p.department
  61.         FROM department d, department p
  62.         WHERE d.mngr_no IS NULL
  63.         AND d.head_dept = p.dept_no;
  64.  
  65.     /* Open the cursor. */
  66.     EXEC SQL
  67.         OPEN to_be_hired;
  68.  
  69.     printf("\n%-25s %-15s %-25s\n\n",
  70.                 "DEPARTMENT", "LOCATION", "HEAD DEPARTMENT");
  71.  
  72.     /*
  73.      *    Select and display all rows.
  74.      */
  75.     while (SQLCODE == 0)
  76.     {
  77.         EXEC SQL
  78.             FETCH to_be_hired INTO :department, :location, :parent_dept;
  79.  
  80.         /* 
  81.          *  If FETCH returns with -100, the processing will jump
  82.          *  to AllDone before the following printf is executed.
  83.          */
  84.         
  85.         printf("%-25s %-15s %-25s\n", department, location, parent_dept);
  86.     }
  87.  
  88.     /*
  89.      *    Close the cursor and release all resources.
  90.      */
  91. AllDone:
  92.     EXEC SQL
  93.         CLOSE to_be_hired;
  94.  
  95.     EXEC SQL
  96.         COMMIT RELEASE;
  97.  
  98. return 0;
  99.  
  100.     /*
  101.      *    Print the error, and exit.
  102.      */
  103. Error:
  104.     isc_print_sqlerror((short)SQLCODE, gds__status);
  105. return 1;
  106. }
  107.  
  108.