%table:ALIAS%Resolves the physical Protheus table name for the specified alias.
AdvPL GuideBeginSql Alias <cAlias> ... EndSqlWrite readable SQL queries directly in AdvPL source code using precompiler substitutions, branch and logical-deletion filters, and declarative type conversion.
Embedded SQL lets you write the SELECT directly between BeginSql and EndSql. During compilation and execution, special expressions delimited by % are transformed to integrate the query with Protheus and DBAccess. Documented resources include %table, %xfilial, %exp, %notDel, %Order, column ... as Date/Logic/Numeric, and %noparser%. By default the query is processed by ChangeQuery(); the opened alias must be closed when processing is complete.
BeginSql Alias <cAlias> ... EndSql%table:ALIAS%Resolves the physical Protheus table name for the specified alias.
%xfilial:ALIAS%Resolves the current branch value appropriate for the specified table.
%exp:expression%Inserts the value of a compatible AdvPL variable or expression into the query.
%notDel%Generates the logical-deletion condition based on D_E_L_E_T_.
%Order:ALIAS%Converts an AdvPL index expression into SQL ordering syntax.
column <field> as <type>Declares Date, Logic, or Numeric result fields for conversion to AdvPL types.
%noparser%Prevents the query from being processed by ChangeQuery() before it is sent to the database. Use only for a clear technical reason.
Opens the query cursor in the alias specified by BeginSql. The result set should be traversed as a query WorkArea and closed with DbCloseArea() when it is no longer needed.
#Include "TOTVS.ch"
User Function ExEmbedded()
Local cAlias := GetNextAlias()
Local cCode := "000001"
// Embedded SQL avoids concatenating text fragments to build the SELECT.
// A1_COD means “Code”, A1_NOME means “Name”, and A1_FILIAL means “Branch”.
BeginSql Alias cAlias
SELECT A1_COD, A1_NOME
FROM %table:SA1% SA1
WHERE A1_FILIAL = %xfilial:SA1%
AND A1_COD = %exp:cCode%
AND SA1.%notDel%
EndSql
While !(cAlias)->(Eof())
ConOut((cAlias)->A1_COD + " - " + (cAlias)->A1_NOME)
(cAlias)->(DbSkip())
EndDo
// The cursor opened by BeginSql must be closed when processing ends.
(cAlias)->(DbCloseArea())
ReturnThe query resolves the physical table, branch, code value, and logical-deletion filter without manually building an SQL string.
#Include "TOTVS.ch"
User Function ExEmbeddedDate()
Local cAlias := GetNextAlias()
Local cPrefix := "NF"
// E2_EMISSAO means “Issue date”, E2_PREFIXO means “Prefix”,
// E2_NUM means “Number”, and E2_FILIAL means “Branch”.
BeginSql Alias cAlias
column E2_EMISSAO as Date
SELECT E2_PREFIXO, E2_NUM, E2_EMISSAO
FROM %table:SE2% SE2
WHERE E2_FILIAL = %xfilial:SE2%
AND E2_PREFIXO = %exp:cPrefix%
AND SE2.%notDel%
EndSql
While !(cAlias)->(Eof())
// E2_EMISSAO is already exposed as an AdvPL date value.
ConOut((cAlias)->E2_PREFIXO + " - " + DToC((cAlias)->E2_EMISSAO))
(cAlias)->(DbSkip())
EndDo
(cAlias)->(DbCloseArea())
ReturnThe column declaration replaces later manual handling of the returned field type.
#Include "TOTVS.ch"
User Function ExLastQuery()
Local cAlias := GetNextAlias()
Local aInfo := {}
// A1_COD means “Code”, A1_NOME means “Name”, and A1_FILIAL means “Branch”.
BeginSql Alias cAlias
SELECT A1_COD, A1_NOME
FROM %table:SA1% SA1
WHERE A1_FILIAL = %xfilial:SA1%
AND SA1.%notDel%
EndSql
// GetLastQuery() provides information about the query that was actually opened.
aInfo := GetLastQuery()
ConOut(aInfo[2]) // Executed SQL
(cAlias)->(DbCloseArea())
ReturnGetLastQuery()[2] exposes the executed SQL string; the complete return also includes the alias, conversion metadata, parser flag, and cursor-opening time.
© 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.