Call Level Interface Guide and Reference

Interactive SQL Example

This example is a modified version of the example contained in the X/Open SQL CLI document. It shows the execution of interactive SQL statements, and follows the flow described in Chapter 2, Writing a DB2 CLI Application.

 
/* From CLI sample adhoc.c */
/* ... */
/*******************************************************************
** process_stmt
** - allocates a statement resources
** - executes the statement
** - determines the type of statement
**   - if there are no result columns, therefore non-select statement
**      - if rowcount > 0, assume statement was UPDATE, INSERT, DELETE
**     else
**      - assume a DDL, or Grant/Revoke statement
**   else
**      - must be a select statement.
**      - display results
** - frees the statement resources
*******************************************************************/
 
int process_stmt( SQLHANDLE hstmt, SQLCHAR * sqlstr ) {
 
    SQLSMALLINT     nresultcols;
    SQLINTEGER      rowcount;
    SQLRETURN       rc;
 
    /* execute the SQL statement in "sqlstr"    */
 
    rc = SQLExecDirect(hstmt, sqlstr, SQL_NTS);
    if (rc != SQL_SUCCESS)
        if (rc == SQL_NO_DATA_FOUND) {
            printf("\nStatement executed without error, however,\n");
            printf("no data was found or modified\n");
            return (SQL_SUCCESS);
        }
        else CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
 
    rc = SQLNumResultCols(hstmt, &nresultcols);
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
 
    /* determine statement type */
    if (nresultcols == 0) {     /* statement is not a select statement */
        rc = SQLRowCount(hstmt, &rowcount);
        if (rowcount > 0) /* assume statement is UPDATE, INSERT, DELETE */
            printf("Statement executed, %ld rows affected\n", rowcount);
        else /* assume statement is GRANT, REVOKE or a DLL statement */
           printf( "Statement completed successful\n" ) ;
    }
    else print_results( hstmt ) ; /* display the result set */
    /* end determine statement type */
 
    /* free statement resources */
 
    rc = SQLFreeStmt( hstmt, SQL_UNBIND ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
 
    rc = SQLFreeStmt( hstmt, SQL_RESET_PARAMS ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
 
    rc = SQLFreeStmt( hstmt, SQL_CLOSE ) ;
    CHECK_HANDLE( SQL_HANDLE_STMT, hstmt, rc ) ;
 
    return( 0 ) ;
 
}                           /* end process_stmt */
 
 
 
/* From the CLI sample utilcli.c */
/* ... */
 
        sqlrc = SQLDescribeCol( hstmt,
                        ( SQLSMALLINT ) ( i + 1 ),
                        colName,
                        sizeof(colName),
                        &colNameLen,
                        &colType,
                        &colSize,
                        &colScale,
                        NULL ) ;
       
 


[ Top of Page | Previous Page | Next Page ]