Usina BRAdvPL Guide
Sign in
← All topics
PRACTICAL GUIDEUnder review

Database operations

RetSqlName(cAlias); xFilial(cAlias); ChangeQuery(cSQL)

Separates reading, writing and access context in Protheus.

Database operationsAdvPL
01 · OVERVIEW

Overview

DBAccess access retains Protheus conventions: physical names from RetSqlName, per-table branches from xFilial and logical deletion through D_E_L_E_T_. SQL reads need explicit fields, type adjustment when necessary and cursor cleanup. Writes must follow process rules, permissions and APIs; a direct UPDATE does not execute business logic. This example only builds SQL without opening a cursor or modifying data. (Sources: refs. 1–1.)

02 · SYNTAX

Syntax

RetSqlName(cAlias); xFilial(cAlias); ChangeQuery(cSQL)
03 · PRACTICAL EXAMPLE

Commented example

#Include "Protheus.ch"

User Function Ed516()
    Local cSQL := "SELECT A1_COD,A1_LOJA FROM "+RetSqlName("SA1")
    // Build only; no query or write is executed.
    cSQL += " WHERE A1_FILIAL = '"+xFilial("SA1")+"'"
    cSQL += " AND D_E_L_E_T_ = ' '"
    ConOut(ValType(cSQL))
Return Nil
Expected result

Prints C. Reading data requires controlled execution and cursor handling. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

BEST PRACTICES
  • Do not concatenate untrusted input into SQL. Validate permissions, branch scope, transactions and write errors. Save and restore work areas.
  • Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Related content

REFERENCES
  1. TOTVS. Desenvolvendo queries no Protheus. TDN. Accessed on 2026-10-27.
Status
Under review
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR
0 approved comment(s)

Comments

There are no approved comments yet.

Sign in with Google or Microsoft to comment.

Powered by Usina Docs · Alpha