Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SAP Sybase SQL Anywhere 16.0 » SQL Anywhere Server - Programming » ODBC support » Ways to execute SQL statements

 

Executing statements with bound parameters

Construct and execute SQL statements using bound parameters to set values for statement parameters at runtime. Bound parameters are also used with prepared statements to provide performance benefits for statements that are executed more than once.

Prérequis

To run the example successfully, you need the following system privileges.

  • INSERT on the Departments table

 Task
  1. Allocate a handle for the statement using SQLAllocHandle.

    For example, the following statement allocates a handle of type SQL_HANDLE_STMT with name stmt, on a connection with handle dbc:

    SQLAllocHandle( SQL_HANDLE_STMT, dbc, &stmt );
  2. Bind parameters for the statement using SQLBindParameter.

    For example, the following lines declare variables to hold the values for the department ID, department name, and manager ID, and for the statement string. They then bind parameters to the first, second, and third parameters of a statement executed using the stmt statement handle.



    #defined DEPT_NAME_LEN 40
    SQLLEN cbDeptID = 0, cbDeptName = SQL_NTS, cbManagerID = 0;
    SQLSMALLINT deptID, managerID;
    SQLCHAR deptName[ DEPT_NAME_LEN + 1 ];
    
    SQLCHAR insertstmt[ STMT_LEN ] =
      "INSERT INTO Departments "
      "( DepartmentID, DepartmentName, DepartmentHeadID ) "
      "VALUES (?, ?, ?)"; 
    
    SQLBindParameter( stmt, 1, SQL_PARAM_INPUT,
        SQL_C_SSHORT, SQL_INTEGER, 0, 0,
        &deptID, 0, &cbDeptID );
    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 );
  3. Assign values to the parameters.

    For example, the following lines assign values to the parameters for the fragment of step 2.

    deptID = 201;
    strcpy( (char * ) deptName, "Sales East" );
    managerID = 902;

    Commonly, these variables would be set in response to user action.

  4. Execute the statement using SQLExecDirect.

    For example, the following line executes the statement string held in insertstmt on the statement handle stmt.

    SQLExecDirect( stmt, insertstmt, SQL_NTS );

Résultat

When built and run, the application executes the SQL statements.

Suivant

The above code fragments do not include error checking. For a complete sample, including error checking, see %SQLANYSAMP16%\SQLAnywhere\ODBCExecute\odbcexecute.cpp.

For more information about SQLExecDirect, see the Microsoft ODBC API Reference at [external link] http://msdn.microsoft.com/en-us/library/ms713611.aspx.

 See also