home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cursor1.zip / cursor1.sqc
Text File  |  2002-11-18  |  4KB  |  132 lines

  1. /******************************************************************************
  2. **
  3. ** Source File Name = cursor.sqc
  4. **
  5. ** Licensed Materials - Property of IBM
  6. **
  7. ** (C) COPYRIGHT International Business Machines Corp. 1995, 2000 
  8. ** All Rights Reserved.
  9. **
  10. ** US Government Users Restricted Rights - Use, duplication or
  11. ** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  12. **
  13. **
  14. ** PURPOSE: This sample program demonstrates the use of a CURSOR.
  15. **          The CURSOR is processed using static SQL.  This program
  16. **          obtains all managers in the STAFF table of the
  17. **          SAMPLE database.
  18. **
  19. ** The file "utilemb.sqc" contains functions for error-checking and
  20. ** rolling back a transaction in case of error. This file must be
  21. ** compiled and its object file linked in to the "cursor" program.
  22. **
  23. ** EXTERNAL DEPENDENCIES :
  24. **       - Ensure existence of database for precompile purposes.
  25. **       - Precompile with the SQL precompiler (PREP in DB2)
  26. **       - Bind to a database (BIND in DB2)
  27. **       - Compile and link with the IBM Cset++ compiler (AIX and OS/2)
  28. **         or the Microsoft Visual C++ compiler (Windows) 
  29. **         or the compiler supported on your platform.
  30. **
  31. ** For more information about these samples see the README file.
  32. **
  33. ** For more information on programming in C, see the:
  34. **   -  "Programming in C and C++" section of the Application Development Guide
  35. ** For more information on building C applications, see the:
  36. **   -  "Building C Applications" section of the Application Building Guide.
  37. **
  38. ** For more information on the SQL language see the SQL Reference.
  39. **
  40. *******************************************************************************/
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include "utilemb.h"
  46.  
  47.  
  48. EXEC SQL INCLUDE SQLCA;
  49.  
  50.  
  51. int main(int argc, char *argv[]) {
  52.  
  53.    EXEC SQL BEGIN DECLARE SECTION;
  54.       char   pname[10];
  55.       short  dept;
  56.       char userid[9];
  57.       char passwd[19];
  58.       char firstname[13];
  59.    EXEC SQL END DECLARE SECTION;
  60.  
  61.  
  62.    printf( "Sample C program: CURSOR \n" );
  63.  
  64.    if (argc == 1) {
  65.       EXEC SQL CONNECT TO sample;
  66.       EMB_SQL_CHECK("CONNECT TO SAMPLE");
  67.    }
  68.    else if (argc == 3) { 
  69.       strcpy (userid, argv[1]);
  70.       strcpy (passwd, argv[2]);
  71.       EXEC SQL CONNECT TO sample USER :userid USING :passwd;
  72.       EMB_SQL_CHECK("CONNECT TO SAMPLE");
  73.    }
  74.    else {
  75.       printf ("\nUSAGE: cursor [userid passwd]\n\n");
  76.       return 1;
  77.    } /* endif */
  78.    
  79.    EXEC SQL DECLARE c1 CURSOR FOR  /* :rk.1:erk. */
  80.             SELECT name, dept FROM staff WHERE job='Mgr';
  81.  
  82.    EXEC SQL OPEN c1;  /* :rk.2:erk. */
  83.    EMB_SQL_CHECK("OPEN CURSOR");
  84.  
  85.    /* start of pmr28801 block */
  86.    EXEC SQL DECLARE c2 CURSOR FOR
  87.         SELECT FIRSTNME 
  88.         FROM employee
  89.         WHERE LASTNAME = 'JOHNSON'
  90.             UNION SELECT FIRSTNME FROM employee
  91.                 WHERE LASTNAME = 'JOHNSON';
  92.    EXEC SQL OPEN c2;
  93.    EMB_SQL_CHECK("OPEN C2 CURSOR");
  94.  
  95.    /* end of pmr28801 block */ 
  96.  
  97.  
  98.    do {
  99.       EXEC SQL FETCH c1 INTO :pname, :dept;  /* :rk.3:erk. */
  100.       if (SQLCODE != 0) break;
  101.  
  102.       printf( "%-10.10s in dept. %2d is a manager\n",
  103.          pname, dept );
  104.    } while ( 1 );
  105.  
  106.    /* pmr28801 start */
  107.  
  108.    do {
  109.      EXEC SQL FETCH c2 INTO :firstname;
  110.      if (SQLCODE != 0) break; 
  111.      printf("C2 Name: - %13s\n", firstname);
  112.    } while (1);
  113.  
  114.    /* pmr28801 end */
  115.  
  116.    EXEC SQL CLOSE c1;  /* :rk.4:erk. */
  117.    EMB_SQL_CHECK("CLOSE CURSOR");
  118.  
  119.    /* pmr28801 start */
  120.  
  121.    EXEC SQL CLOSE c2;
  122.    EMB_SQL_CHECK("CLOSE C2");
  123.  
  124.    /* pmr28801 end */
  125.  
  126.    EXEC SQL CONNECT RESET;
  127.    EMB_SQL_CHECK("CONNECT RESET");
  128.    return 0;
  129. }
  130. /* end of program : CURSOR.SQC */
  131.  
  132.