cSqlExprCharacter expression, constant, or variable containing a SELECT query to be executed through TOPCONN.
AdvPL GuideTCQUERY <cSqlExpr> ALIAS <cAlias> [NEW]Executes a SELECT query through the TOPCONN RDD and opens the result in an AdvPL WorkArea.
TCQUERY opens the result of a read-only SQL query under an AdvPL alias through the TOPCONN RDD. At compile time, the command is translated into operations based on DbUseArea() and TCGenQry(). NEW should be preferred to create a new WorkArea and avoid unintentionally closing the current area. The resulting cursor is intended for sequential reading and should not be treated as an editable ISAM table.
TCQUERY <cSqlExpr> ALIAS <cAlias> [NEW]cSqlExprCharacter expression, constant, or variable containing a SELECT query to be executed through TOPCONN.
ALIAS cAliasAlias name under which the result set will be opened. It may be a literal or an expression.
NEWOpens the query in a new WorkArea. Official documentation recommends it to avoid closing the current area.
Does not directly return a value. The query result becomes available under the supplied alias as a read cursor.
#Include "TOTVS.ch"
#Include "TopConn.ch"
User Function ExTCQuery()
Local cAlias := GetNextAlias()
Local cQuery := ""
// A1_COD means “Code”, A1_NOME means “Name”, and A1_FILIAL means “Branch”.
// D_E_L_E_T_ is the technical field used to control logical deletion.
cQuery := "SELECT A1_COD, A1_NOME "
cQuery += "FROM " + RetSqlName("SA1") + " "
cQuery += "WHERE A1_FILIAL = '" + xFilial("SA1") + "' "
cQuery += "AND D_E_L_E_T_ = ' ' "
cQuery += "ORDER BY A1_COD"
// ChangeQuery() adapts the statement to supported databases when required.
cQuery := ChangeQuery(cQuery)
// NEW preserves the WorkArea that was current before this query.
TCQUERY (cQuery) ALIAS (cAlias) NEW
While !(cAlias)->(Eof())
ConOut((cAlias)->A1_COD + " - " + (cAlias)->A1_NOME)
(cAlias)->(DbSkip())
EndDo
// The query alias occupies a WorkArea and should be closed when finished.
(cAlias)->(DbCloseArea())
ReturnThe query uses SA1’s physical table name, filters the branch and logical deletion, opens a dynamic alias in a new WorkArea, and releases the cursor at the end.
#Include "TopConn.ch"
// More readable form for opening the query:
TCQUERY (cQuery) ALIAS QRY NEW
QRY->(DbCloseArea())
// Conceptually, TCQUERY combines TOPCONN opening with TCGenQry().
// Internal implementation may evolve; use the documented command syntax.TCQUERY directly expresses the intent to open a query as a WorkArea without making application code depend on opening details.
© 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.