Executing prepared statements

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

StepsTo execute a prepared SQL statement

  1. Prepare the statement using SQLPrepare.

    For example, the following code fragment illustrates how to prepare an insert statement:

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

    where:

  2. Set statement parameter values using SQLBindParameter.

    For example, the following function call sets the value of the dept_id variable:

    SQLBindParameter( stmt,
       1,
       SQL_PARAM_INPUT, 
       SQL_C_SHORT, 
       SQL_INTEGER,  
       0,           
       0,             
       &sDeptID, 
       0,        
       &cbDeptID);
    

    where:

  3. Bind the other two parameters and assign values to sDeptId:

    SQLBindParameter( stmt, 2, SQL_PARAM_INPUT,
       SQL_C_CHAR, SQL_CHAR, DEPT_NAME_LEN, 0,
       deptname, 0,&cbDeptName);
    
    SQLBindParameter( stmt, 3, SQL_PARAM_INPUT,
       SQL_C_SSHORT, SQL_INTEGER, 0, 0,
       &managerID, 0, &cbManagerID);
    
  4. Execute the statement:

    retcode = SQLExecute( stmt);
    

    You can repeat steps 2 through 4 multiple times.

  5. Drop the statement using SQLFreeHandle.

    Dropping the statement frees resources associated with the statement itself.