Using dynamic commands

When you write an application using dynamic commands, consider the following:

This 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.