/*******************************************************************************
**
** Source File Name = setcurs.c 1.4
**
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 1995, 1999
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
**
** PURPOSE :
** Example of directly executing a SELECT and positioned UPDATE SQL statement.
** Two statment handles are used, and SQLSetCursor is used to create
** an application defined cursor name to be used in the SQL statement.
**
** For more information about these samples see the README file.
**
** For more information on programming in CLI see the:
** - "Building CLI Applications" section of the Application Building Guide, and the
** - CLI Guide and Reference.
**
** For more information on the SQL language see the SQL Reference.
**
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlcli1.h>
#include "samputil.h" /* Header file for CLI sample code */
/* For the Macintosh environment when generating 68K applications */
#ifdef DB268K
/* Need to include ASLM for 68K applications */
#include <LibraryManager.h>
#endif
#define MAX_STMT_LEN 255
/*
Global Variables for user id and password.
To keep samples simple, not a recommended practice.
*/
extern SQLCHAR server[SQL_MAX_DSN_LENGTH + 1] ;
extern SQLCHAR uid[MAX_UID_LENGTH + 1] ;
extern SQLCHAR pwd[MAX_PWD_LENGTH + 1] ;
/* main */
int main( int argc, char * argv[] ) {
SQLHANDLE henv, hdbc, hstmt1, hstmt2 ;
SQLRETURN rc, rc2 ;
/*--> SQLL1X09.SCRIPT */
SQLCHAR * sqlstmt = ( SQLCHAR * )
"SELECT name, job FROM staff WHERE job = 'Clerk' FOR UPDATE OF job" ;
/*<-- */
SQLCHAR updstmt[MAX_STMT_LEN + 1];
struct { SQLINTEGER ind;
SQLCHAR s[10];
} name;
struct { SQLINTEGER ind;
SQLCHAR s[6];
} job;
SQLCHAR newjob[6];
/* For the Macintosh environment when generating 68K applications */
#ifdef DB268K
/* Before making any API calls for 68K environment, need to initialize the
Library Manager */
InitLibraryManager(0,kCurrentZone,kNormalMemory);
atexit(CleanupLibraryManager);
#endif
/* macro to initalize server, uid and pwd */
INIT_UID_PWD ;
/* allocate an environment handle */
rc = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv ) ;
if ( rc != SQL_SUCCESS ) return( terminate( henv, rc ) ) ;
/* allocate a connect handle, and connect */
rc = DBconnect( henv, &hdbc ) ;
if ( rc != SQL_SUCCESS ) return( terminate( henv, rc ) ) ;
rc = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt1 ) ;
CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;
/*--> */
/* allocate second statement handle for update statement */
rc = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt2 ) ;
CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;
/* Set Cursor for the SELECT statement's handle */
rc = SQLSetCursorName(hstmt1, (SQLCHAR *)"JOBCURS", SQL_NTS);
CHECK_HANDLE( SQL_HANDLE_STMT, hstmt1, rc ) ;
rc = SQLExecDirect(hstmt1, sqlstmt, SQL_NTS);
CHECK_HANDLE( SQL_HANDLE_STMT, hstmt1, rc ) ;
/* bind name to first column in the result set */
rc = SQLBindCol(hstmt1, 1, SQL_C_CHAR, (SQLPOINTER) name.s, 10,
&name.ind);
CHECK_HANDLE( SQL_HANDLE_STMT, hstmt1, rc ) ;
/* bind job to second column in the result set */
rc = SQLBindCol(hstmt1, 2, SQL_C_CHAR, (SQLPOINTER) job.s, 6,
&job.ind);
CHECK_HANDLE( SQL_HANDLE_STMT, hstmt1, rc ) ;
printf("Job Change for all clerks\n");
while ((rc = SQLFetch(hstmt1)) == SQL_SUCCESS) {
printf("Name: %-9.9s Job: %-5.5s \n", name.s, job.s);
printf("Enter new job or return to continue\n");
gets((char *)newjob);
if (newjob[0] != '\0') {
sprintf((char *)updstmt,
"UPDATE staff set job = '%s' where current of JOBCURS",
newjob);
rc2 = SQLExecDirect(hstmt2, updstmt, SQL_NTS);
CHECK_HANDLE( SQL_HANDLE_STMT, hstmt2, rc2 ) ;
}
}
if (rc != SQL_NO_DATA_FOUND)
CHECK_HANDLE( SQL_HANDLE_STMT, hstmt1, rc ) ;
/*<-- */
/* free statement resources */
rc = SQLEndTran( SQL_HANDLE_DBC, hdbc, SQL_COMMIT ) ;
CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;
rc = SQLFreeHandle( SQL_HANDLE_STMT, hstmt1 ) ;
CHECK_HANDLE( SQL_HANDLE_STMT, hstmt1, rc ) ;
rc = SQLFreeHandle( SQL_HANDLE_STMT, hstmt2 ) ;
CHECK_HANDLE( SQL_HANDLE_STMT, hstmt2, rc ) ;
printf( "\n>Disconnecting .....\n" ) ;
rc = SQLDisconnect( hdbc ) ;
CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;
rc = SQLFreeHandle( SQL_HANDLE_DBC, hdbc ) ;
CHECK_HANDLE( SQL_HANDLE_DBC, hdbc, rc ) ;
rc = SQLFreeHandle( SQL_HANDLE_ENV, henv ) ;
if ( rc != SQL_SUCCESS ) return( terminate( henv, rc ) ) ;
return( SQL_SUCCESS ) ;
} /* end main */