cSqlExprConstant or variable SQL expression executed through TOPCONN.
AdvPL GuideTCQUERY <cSqlExpr> ALIAS <cAlias> [NEW]Executes an SQL query through the TOPCONN RDD and exposes the result in an AdvPL work area.
TCQUERY executes an SQL expression against a relational database through the TOPCONN RDD and opens the result under the supplied alias. During compilation it is translated into DbUseArea() and TCGenQry() calls. NEW should be used to open a new work area and avoid closing the current area and any table already open in it.
TCQUERY <cSqlExpr> ALIAS <cAlias> [NEW]cSqlExprConstant or variable SQL expression executed through TOPCONN.
ALIAS cAliasName of the work area where the query result is opened. It may be a literal or expression.
NEWOpens the query in a new WorkArea. Official documentation recommends always using it to avoid closing the current area.
#Include "TOTVS.ch"
#Include "TopConn.ch"
User Function ExTCQuery()
Local cAlias := GetNextAlias()
Local cQuery := ""
cQuery := "SELECT A1_COD, A1_NOME "
cQuery += "FROM " + RetSqlName("SA1") + " "
cQuery += "WHERE D_E_L_E_T_ = ' ' "
cQuery += "ORDER BY A1_COD"
cQuery := ChangeQuery(cQuery)
TCQUERY (cQuery) ALIAS (cAlias) NEW
While !(cAlias)->(Eof())
ConOut((cAlias)->A1_COD + " - " + ;
(cAlias)->A1_NOME)
(cAlias)->(DbSkip())
EndDo
(cAlias)->(DbCloseArea())
ReturnThe query uses SA1’s physical name, receives compatibility adjustments, opens a new WorkArea and closes the alias when finished.
#Include "TopConn.ch"
// Forma recomendada pela legibilidade:
TCQUERY (cQuery) ALIAS QRY NEW
QRY->(DbCloseArea())
// Internamente, o comando combina operações equivalentes a:
// DbUseArea(.T., "TOPCONN", ;
// TCGenQry(,, cQuery), "QRY", .T., .T.)TCQUERY simplifies opening with DbUseArea() and TCGenQry() while keeping source intent clear.
© 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.