Sybase Technical Library - Product Manuals Home
[Search Forms] [Previous Section with Hits] [Next Section with Hits] [Clear Search] Expand Search

Error checking in ODBC [Table of Contents] Working with result sets

[-] Chapter 4: ODBC Programming
[-] Using prepared statements in ODBC


Using prepared statements in ODBC

Prepared statements provide performance advantages for statements that are used repeatedly. ODBC provides a full set of functions for using prepared statements.

For an introduction to prepared statements, see Preparing statements .


>> To execute a prepared statement:
  1. You prepare the statement using SQLPrepare . The following code fragment illustrates how to prepare an INSERT statement:

    SQLRETURN   retcode;
    SQLHSTMT    hstmt;
    
    
    retcode = SQLPrepare(hstmt,
                "INSERT 
                 INTO department 
                 (dept_id, dept_name, dept_head_id ) 
                 VALUES (?, ?, ?,)", 
              SQL_NTS);

    In this example:

  2. You set statement parameter values using SQLBindParameter . For example, the following function call sets the value of the dept_id variable:

    SQLBindParameter(hstmt, 
                     1, 
                     SQL_PARAM_INPUT, 
                     SQL_C_SSHORT,
                     SQL_INTEGER, 
                     0, 
                     0, 
                     &sDeptID, 
                     0, 
                     &cbDeptID);

In this example:

For more information, see the ODBC SDK documentation.


Error checking in ODBC [Table of Contents] Working with result sets