SQLPrepare - cSQLStatement

Prepare a statement that is passed for execution.

Type: Procedure

Parameters: String sStatement

ParameterDescription
sStatementStatement to prepare


Syntax
Procedure SQLPrepare String sStatement

Call: Send SQLPrepare sStatement


Description

Prepare the passed statement for execution. Prepared execution is an efficient way to execute a query more than once. Preparing a statement will create the statement's access plan. Calling SQLExecute will execute the prepared statement. Prepared execution is faster than direct execution for statements executed more than once, primarily because the access plan is created only once.

See Parameterized Queries for more information on parametrized queries and prepared execution.

Resets piLastArgument to 0.

Sample

Handle hoSQLMngr
Handle hdbc hstmt hstmt2
integer iFetchResult
string sName sState sCrLim sBal

Object oSQLHandler Is A cSQLHandleManager
    Move Self To hoSQLMngr
End_Object

Open Customer
Get SQLFileConnect Of hoSQLMngr Customer.File_number To hdbc
Get SQLOpen Of hDbc To hStmt
Send SQLPrepare Of hstmt "Select Name, State, Credit_Limit, Balance from Customer"
Send SQLExecute Of hstmt
Showln ">> Customer Before Update <<"
Repeat
    Get SQLFetch Of hstmt To iFetchResult
    If (iFetchResult <> 0) Begin
        Get SQLColumnValue of hstmt 1 to sName
        Get SQLColumnValue of hstmt 2 to sState
        Get SQLColumnValue of hstmt 3 to sCrLim
        Get SQLColumnValue of hstmt 4 to sBal
        Showln sName ", " sState ", " sCrLim ", " sBal
    End
Until (iFetchResult = 0)

Showln
Get SQLOpen Of hdbc To hstmt2
Send SQLExecDirect To hstmt2 "update customer set credit_limit=5000 where state='CA' and purchases > 1000"
Send SQLExecute Of hstmt
Showln ">> Customer After Update <<"
Repeat
    Get SQLFetch Of hstmt To iFetchResult
    If (iFetchResult <> 0) Begin
        Get SQLColumnValue of hstmt 1 to sName
        Get SQLColumnValue of hstmt 2 to sState
        Get SQLColumnValue of hstmt 3 to sCrLim
        Get SQLColumnValue of hstmt 4 to sBal
        Showln sName ", " sState ", " sCrLim ", " sBal
    End
Until (iFetchResult = 0)

Send SQLClose Of hstmt
Send SQLDisconnect Of hdbc