GetNextAlias()Generates a temporary result-set alias and prevents collisions.
AdvPL GuidecQuery → ChangeQuery() → TCQUERY / TCGenQry() → WorkArea → DbCloseArea()Build portable and efficient SQL queries in Protheus with DBAccess, dynamic aliases and correct result-set handling.
DBAccess connects Protheus to supported SQL databases and exposes ISAM and native SQL access. For data sets, a well-designed query usually reduces client-side scans, processing and network traffic. Queries may be assembled as strings or written with Embedded SQL; both reach DBAccess.
cQuery → ChangeQuery() → TCQUERY / TCGenQry() → WorkArea → DbCloseArea()GetNextAlias()Generates a temporary result-set alias and prevents collisions.
RetSqlName()Maps a Protheus logical alias to its physical database table name.
ChangeQuery()Adapts SQL syntax to supported databases.
TcSetField()Adjusts non-character result fields to expected AdvPL types.
SqlOrder()Converts an AdvPL index expression for SQL ORDER BY.
xFilial()Supplies the correct branch value for a table filter.
DToS()Produces YYYYMMDD for date comparisons when appropriate.
#Include "TOTVS.ch"
#Include "TopConn.ch"
// See the Portuguese example; localize only user-facing text.Uses the physical table name, branch and deletion filters, compatibility conversion, dynamic alias and explicit cleanup.
cQuery := "SELECT R_E_C_N_O_ RECNO "
cQuery += "FROM " + RetSqlName("SA1") + " SA1 "
cQuery += "WHERE A1_FILIAL = '" + xFilial("SA1") + "' "
cQuery += "AND D_E_L_E_T_ = ' '"
cQuery := ChangeQuery(cQuery)
TCQUERY (cQuery) ALIAS (cAlias) NEW
While !(cAlias)->(Eof())
SA1->(DbGoTo((cAlias)->RECNO))
// Leia ou trate o registro real já posicionado.
(cAlias)->(DbSkip())
EndDo
(cAlias)->(DbCloseArea())The result set identifies rows and DbGoTo() positions the physical table through R_E_C_N_O_.
© 2026 Usina.BR. Todos os direitos reservados. TOTVS, Protheus, Microsiga e seus respectivos logotipos são marcas de titularidade da TOTVS S.A. AdvPL é uma linguagem de programação desenvolvida pela TOTVS. O AdvPL Guia é um projeto independente, sem vínculo, representação, patrocínio ou afiliação com a TOTVS S.A., suas franquias ou representantes.