BeginSql AliasStarts the block and names the result cursor.
AdvPL GuideBeginSql Alias cAlias ... EndSqlWrite readable SQL directly in AdvPL source code with precompiler substitutions and declarative type conversion.
Embedded SQL replaces manual string concatenation with a BeginSql/EndSql block. Literal SQL is preserved while percent-delimited expressions resolve Protheus tables, branches, values, deletion filters and index orders. The cursor opens in the supplied alias and ChangeQuery() runs by default.
BeginSql Alias cAlias ... EndSqlBeginSql AliasStarts the block and names the result cursor.
column ... as Date | Logical | NumericDeclares date, logical or numeric result conversion.
%table:ALIAS%Resolves the physical table through RetSqlName().
%xfilial:ALIAS%Inserts the branch value for the table.
%notDel%Generates the logical-deletion filter.
%exp:expressão%Evaluates a character, date, numeric or logical AdvPL value.
%Order:ALIAS,nOrdem%Converts an AdvPL index order through SqlOrder().
%temp-table:cNome%Inserts a temporary-table name stored in a variable.
%noparser%Skips ChangeQuery(); use only for an intentional DBMS-specific case.
EndSqlEnds the block and opens the cursor.
// See the Portuguese example; only user-facing text should be localized.The precompiler resolves the table, branch, value, deletion filter and order.
Local cAlias := GetNextAlias()
Local aInfo := {}
BeginSql Alias cAlias
column E2_EMISSAO as Date
column E2_VALOR as Numeric(16, 2)
SELECT E2_EMISSAO, E2_VALOR
FROM %table:SE2% SE2
WHERE E2_FILIAL = %xfilial:SE2%
AND SE2.%notDel%
EndSql
aInfo := GetLastQuery()
ConOut("Tempo de abertura: " + cValToChar(aInfo[5]))
(cAlias)->(DbCloseArea())Fields reach AdvPL with the declared types and GetLastQuery() exposes execution details.
© 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.