When you write an application using dynamic commands, consider the following:
The SQL statement being prepared must not be a select statement. Although Open Server allows a select statement to be prepared and a cursor to be opened on the prepared statement, the DB2 access service does not support this capability.
Use DB2 syntax for the statement you are preparing when you are in passthrough transformation mode.
Following is an example of a code fragment that prepares and executes an insert statement.
/* ** This sample code fragment illustrates the CT-Library calls ** used to prepare and execute a SQL statement. Error handling ** and other details are omitted for the sake of brevity. ** NOTE: Ellipses in the following represent code that you must supply. */ /* ** Prepare the statement. The statement we will prepare is: ** INSERT INTO TEST VALUES (?, ?, ?, ?). ** We will name the dynamic statement DYN1. */ retcode = ct_dynamic( cmd, CS_PREPARE, "DYN1", CS_NULLTERM, "INSERT INTO TEST VALUES (?, ?, ?, ?)", CS_NULLTERM ); /* ** Send the batch and check results. */ retcode = ct_send( cmd ); retcode = handleresults( cmd ); /* ** Now execute the prepared statement with a set of parameter ** values. Allocate a CS_DATAFMT structure for each parameter. */ dfmt = malloc( 4 * sizeof(struct CS_DATAFMT) ); /* ** Fill in the structure and set the datalength, null indicator, ** and data value for each parameter. */ dfmt[0].datatype = CS_CHAR_TYPE; dfmt[0].status = CS_INPUTVAL; dfmt[0].maxlength = strlen( "col1val" ); dataptr[0] = "col1val"; datalen[0] = strlen( "col1val" ); nullind[0] = 0; ... /* ** Execute the statement. */ retcode = ct_dynamic( cmd, CS_EXECUTE, "DYN1", CS_NULLTERM, NULL, CS_UNUSED ); /* ** Describe and send the parameters. */ for (i=0; i<4; i++) { retcode = ct_param( cmd, &dfmt[i], dataptr[0], datalen[i], nullind[i] ); } /* ** Send the batch and check results. */ retcode = ct_send( cmd ); retcode = handleresults( cmd );
For more information about SQL processing, see the IBM DB2 SQL Reference manual.
Copyright © 2005. Sybase Inc. All rights reserved. |