home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cursor1.zip
/
cursor1.sqc
Wrap
Text File
|
2002-11-18
|
4KB
|
132 lines
/******************************************************************************
**
** Source File Name = cursor.sqc
**
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 1995, 2000
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
**
** PURPOSE: This sample program demonstrates the use of a CURSOR.
** The CURSOR is processed using static SQL. This program
** obtains all managers in the STAFF table of the
** SAMPLE database.
**
** The file "utilemb.sqc" contains functions for error-checking and
** rolling back a transaction in case of error. This file must be
** compiled and its object file linked in to the "cursor" program.
**
** EXTERNAL DEPENDENCIES :
** - Ensure existence of database for precompile purposes.
** - Precompile with the SQL precompiler (PREP in DB2)
** - Bind to a database (BIND in DB2)
** - Compile and link with the IBM Cset++ compiler (AIX and OS/2)
** or the Microsoft Visual C++ compiler (Windows)
** or the compiler supported on your platform.
**
** For more information about these samples see the README file.
**
** For more information on programming in C, see the:
** - "Programming in C and C++" section of the Application Development Guide
** For more information on building C applications, see the:
** - "Building C Applications" section of the Application Building Guide.
**
** For more information on the SQL language see the SQL Reference.
**
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utilemb.h"
EXEC SQL INCLUDE SQLCA;
int main(int argc, char *argv[]) {
EXEC SQL BEGIN DECLARE SECTION;
char pname[10];
short dept;
char userid[9];
char passwd[19];
char firstname[13];
EXEC SQL END DECLARE SECTION;
printf( "Sample C program: CURSOR \n" );
if (argc == 1) {
EXEC SQL CONNECT TO sample;
EMB_SQL_CHECK("CONNECT TO SAMPLE");
}
else if (argc == 3) {
strcpy (userid, argv[1]);
strcpy (passwd, argv[2]);
EXEC SQL CONNECT TO sample USER :userid USING :passwd;
EMB_SQL_CHECK("CONNECT TO SAMPLE");
}
else {
printf ("\nUSAGE: cursor [userid passwd]\n\n");
return 1;
} /* endif */
EXEC SQL DECLARE c1 CURSOR FOR /* :rk.1:erk. */
SELECT name, dept FROM staff WHERE job='Mgr';
EXEC SQL OPEN c1; /* :rk.2:erk. */
EMB_SQL_CHECK("OPEN CURSOR");
/* start of pmr28801 block */
EXEC SQL DECLARE c2 CURSOR FOR
SELECT FIRSTNME
FROM employee
WHERE LASTNAME = 'JOHNSON'
UNION SELECT FIRSTNME FROM employee
WHERE LASTNAME = 'JOHNSON';
EXEC SQL OPEN c2;
EMB_SQL_CHECK("OPEN C2 CURSOR");
/* end of pmr28801 block */
do {
EXEC SQL FETCH c1 INTO :pname, :dept; /* :rk.3:erk. */
if (SQLCODE != 0) break;
printf( "%-10.10s in dept. %2d is a manager\n",
pname, dept );
} while ( 1 );
/* pmr28801 start */
do {
EXEC SQL FETCH c2 INTO :firstname;
if (SQLCODE != 0) break;
printf("C2 Name: - %13s\n", firstname);
} while (1);
/* pmr28801 end */
EXEC SQL CLOSE c1; /* :rk.4:erk. */
EMB_SQL_CHECK("CLOSE CURSOR");
/* pmr28801 start */
EXEC SQL CLOSE c2;
EMB_SQL_CHECK("CLOSE C2");
/* pmr28801 end */
EXEC SQL CONNECT RESET;
EMB_SQL_CHECK("CONNECT RESET");
return 0;
}
/* end of program : CURSOR.SQC */