cQuerySQL query containing ? placeholders for values that will be bound later.
AdvPL GuideFWExecStatement():New( <cQuery> ) --> oStatementExecutes parameterized SQL queries with value binding, alias-based result sets, and methods inherited from FWPreparedStatement.
FWExecStatement derives from FWPreparedStatement and encapsulates the Framework query execution/cache concepts. It lets you build SQL with ? placeholders, bind typed values, and execute through OpenAlias() or ExecScalar(). Official documentation states that it is available starting with LIB label 20211116 and that its bind behavior follows TCGenQry2 rules and limitations.
FWExecStatement():New( <cQuery> ) --> oStatementcQuerySQL query containing ? placeholders for values that will be bound later.
oStatement:OpenAlias( [cAlias], [cLifeTime], [cTimeout] ) --> cAliascAliasCaractereOptionalAlias que será criado para o resultado.
cLifeTimeCaractereOptionalConfiguração de tempo de vida da consulta no cache da DBAPI.
cTimeoutCaractereOptionalConfiguração de timeout relacionada ao cache da consulta.
cAlias, caractere. Alias em que o resultado foi aberto.
oStatement:ExecScalar( <cColumn>, [cLifeTime], [cTimeout] ) --> xValuecColumnCaractereRequiredNome da coluna que deve ser retornada.
cLifeTimeCaractereOptionalConfiguração de tempo de vida no cache da DBAPI.
cTimeoutCaractereOptionalConfiguração de timeout relacionada ao cache.
xValue, variante. Valor obtido da coluna informada.
oStatement:SetString( <nParam>, <cValue> )oStatement:SetDate( <nParam>, <dDate> )oStatement:SetBoolean( <nParam>, <lValue>, [lProtheus] )oStatement:SetIn( <nParam>, <aValues> )oStatement:SetUnsafe( <nParam>, <xValue> )oStatement:GetFixQuery() --> cQueryoStatement:Destroy()The constructor returns a FWExecStatement object. Query execution occurs later through OpenAlias() or ExecScalar().
#Include "TOTVS.ch"
User Function ExFWExec()
Local cQuery := ""
Local cAlias := ""
Local oStmt
// A1_COD means “Code”, A1_NOME means “Name”, and A1_FILIAL means “Branch”.
cQuery := "SELECT A1_COD, A1_NOME FROM " + RetSqlName("SA1")
cQuery += " WHERE A1_FILIAL = ? AND A1_COD = ? AND D_E_L_E_T_ = ' '"
cQuery := ChangeQuery(cQuery)
oStmt := FWExecStatement():New(cQuery)
// Parameters start at 1 and follow the order of the ? placeholders.
oStmt:SetString(1, xFilial("SA1"))
oStmt:SetString(2, "000001")
cAlias := oStmt:OpenAlias()
While !(cAlias)->(Eof())
ConOut((cAlias)->A1_COD + " - " + (cAlias)->A1_NOME)
(cAlias)->(DbSkip())
EndDo
// Close the alias first, then release the statement object.
(cAlias)->(DbCloseArea())
oStmt:Destroy()
ReturnThe query binds branch and code values to the placeholders and opens the result in a WorkArea.
User Function ExFWExecIn()
Local cQuery := "SELECT A1_COD, A1_NOME FROM " + RetSqlName("SA1")
Local cAlias := ""
Local oStmt
Local aCodes := {"000001", "000003"}
// A1_COD means “Code”; A1_FILIAL means “Branch”.
cQuery += " WHERE A1_FILIAL = ? AND A1_COD IN (?) AND D_E_L_E_T_ = ' '"
cQuery := ChangeQuery(cQuery)
oStmt := FWExecStatement():New(cQuery)
oStmt:SetString(1, xFilial("SA1"))
// SetIn() is inherited from FWPreparedStatement and receives an array.
oStmt:SetIn(2, aCodes)
cAlias := oStmt:OpenAlias()
// Alias processing is omitted to emphasize parameter binding.
(cAlias)->(DbCloseArea())
oStmt:Destroy()
ReturnSetIn() binds the code list to the second query placeholder.
User Function ExFWScalar()
Local cQuery := "SELECT COUNT(*) QTY FROM " + RetSqlName("SA1")
Local oStmt
Local nQty := 0
// A1_FILIAL means “Branch”.
cQuery += " WHERE A1_FILIAL = ? AND D_E_L_E_T_ = ' '"
cQuery := ChangeQuery(cQuery)
oStmt := FWExecStatement():New(cQuery)
oStmt:SetString(1, xFilial("SA1"))
// ExecScalar() avoids opening an alias when only one column/value is needed.
nQty := oStmt:ExecScalar("QTY")
oStmt:Destroy()
ConOut("Quantity: " + CValToChar(nQty))
ReturnExecScalar() directly returns the aggregate column value.
© 2026 Usina.BR. All rights reserved. TOTVS, Protheus, Microsiga and their respective logos are trademarks of their owners. AdvPL Guide is independent and uses limited, attributed excerpts justified by educational purposes. See the Terms of Use. Terms of Use
Comments
There are no approved comments yet.
Sign in with Google or Microsoft to comment.