Previous Topic: Declare VariablesNext Topic: Execute SQL Statement


Construct the ODBC/JDBC Statements

This procedure gives you an idea about constructing ODBC/JDBC statements.

Follow these steps:

  1. Initialize the statement handle or object with SQL query string. This is done in the Code section of the inline code statement.

    Note: The string literal “IEFDB” refers to the data source name.

    Note: The string literal “IEFDB” refers to the data source name.

  2. Binding the columns or parameters using host variables provides a way to exchange data between the data source and the application. When an SQL statement is executed, the bounded parameters will be read and the bounded columns will be retrieved. The application should take the responsibility to read from and/or write to the host variables. Sometimes, more than one set of host variables are needed (for example, SELECT + UPDATE).

    Binding the columns in a SELECT SQL statement using host variables applies to C/ODBC only. Result sets are used to retrieve data in C#/ADO.NET and Java/JDBC.

    Use the SQLBindCol ODBC API to bind columns with known host variables. For more information, see the MSDN documentation on ODBC APIs.

    /* To execute SQL (SELECT) query */
    ret_code = SQLBindCol(read_stmt.hstmt, 1, SQL_C_SLONG, &hv_number, 
    			sizeof(hv_number), &cbData);
    ret_code = SQLBindCol(read_stmt.hstmt, 2, SQL_C_CHAR, &hv_name, 
    			sizeof(hv_name), &cbData);
    ret_code = SQLBindCol(read_stmt.hstmt, 3, SQL_C_CHAR, &hv_work_phone, 
    			sizeof(hv_work_phone), &hvind_work_phone);
    
    

    Note: The column index starts from one. Non-query SQL statements, such as INSERT / UPDATE / DELETE, do not need binding columns.

    Note: Refer to Table 1 in Appendix section about SQL C types for various Gen data types.

    Bind the parameters using host variables, if any. All types of SQL statements need parameter binding.

    Note: The parameter index starts from one. Column and Parameter indices are independent of each other. When required, specify the OWNER of DB physical table at the third parameter of TiodbcParameter API.

    Note: Refer to Table 1 in Appendix section about SQL C types for various Gen data types.

    Note: The parameter index starts from zero.

    Note: Refer to Table 2 in Appendix section about bind methods for various Gen data types.

    Note: The parameter index starts from one.

    Note: Refer to Table 3 in Appendix section about bind methods for various Gen data types.