Skip to content
Agentmart
Submit a tool

advpl-embedded-sql

SQL embarcado em ADVPL — BeginSql/EndSql, TCQuery, macros obrigatórias %notDel%, %xfilial%, %table%, %exp%, %Order%, 6 restrições de performance e segurança.

Skill by replybr

IndexedMIT
Write a review

Chalk Labs

Need help deploying advpl-embedded-sql?

Chalk Labs sets up agents like this inside real teams: connected to your tools, tested on your data, with a human in the loop where it matters.

Book a call

Install

Works with Claude Code, Claude apps, Codex, Cursor, Gemini CLI, GitHub Copilot, VS Code, OpenCode, Goose, Windsurf. Read the repository before you give any tool access to your data.

npx agentmarket install skill/replybr-advpl-embedded-sql

Asks which of your tools to add it to (Claude Code, Codex, Cursor, VS Code, Windsurf, Gemini CLI, Claude Desktop) and shows the change first. See https://agentmart.dev/cli.

Get the weekly diff

The weekly diff: one email a week with new agent tools, notable updates and grade changes, plus security alerts for tools you follow. Double opt-in, one-click unsubscribe.

We use your email only for this newsletter. Privacy

About

SQL embarcado em ADVPL — BeginSql/EndSql, TCQuery, macros obrigatórias %notDel%, %xfilial%, %table%, %exp%, %Order%, 6 restrições de performance e segurança.

Use cases: backend

No reviews yet. Installed it? Reviews come from verified installers only. How reviews work

Permissions

What this skill can do and what it asks for, from its SKILL.md. Checked Oct 25, 2026.

Tools
No tools found
Secrets
None asked for
Install scripts
None found

Network hosts it talks to 1

  • codeload.github.com · found in install command

Files it touches

  • ~/.claude/skills/advpl-embedded-sql

What's inside

Skill name
advpl-embedded-sql
Description
SQL embarcado em ADVPL — BeginSql/EndSql, TCQuery, macros obrigatórias %notDel%, %xfilial%, %table%, %exp%, %Order%, 6 restrições de performance e segurança.
License
MIT
Instructions
156 lines in SKILL.md · view on GitHub

Agent Skills spec issues

  • name: missing

Files (2)

  • SKILL.md
  • reference.md

SKILL.md (first lines)

# advpl-embedded-sql — SQL nativo em ADVPL

ADVPL/TLPP suporta SQL embarcado direto no fonte com **substituição por macros** que garantem multi-empresa, multi-filial, anti-SQL-injection e portabilidade entre DBMS (Oracle, SQL Server, Postgres, DB2).

Existem duas formas principais:

1. **`BeginSql alias <ALIAS> ... EndSql`** — recomendado, suporta macros.
2. **`TCQuery cSql NEW ALIAS <ALIAS>`** — antigo, sem macros (string crua).

## Quando usar

- Edit/criação de qualquer query SQL no fonte ADVPL.
- Refator de SQL legado (`TCQuery` → `BeginSql`).
- Revisão de regra `PERF-001..006` ou `SEC-001`.
- Antes de testar performance — sem macros, queries dão problema em produção multi-empresa.

## As 5 macros obrigatórias

| Macro                | O que faz                                            | Output           | Safe injection |
|----------------------|------------------------------------------------------|------------------|----------------|
| `%table:TABLE%`      | Resolve nome físico (`SA1010` em vez de `SA1`)       | identifier       | sim            |
| `%xfilial:TABLE%`    | Valor atual da filial, já entre aspas                | string literal   | sim            |
| `%notDel%`           | Filtro `D_E_L_E_T_ <> '*'` (registros não-deletados) | predicate        | sim            |
| `%exp:VAR%`          | Binda variável ADVPL com escaping automático         | scalar literal   | sim            |
| `%Order:TABLE%`      | Resolve `ORDER BY` da chave primária da tabela       | columns          | sim            |

**Concatenação com `+` para incluir variável é proibida** — viola `SEC-001` (crítico, SQL injection).

## Exemplo correto — BeginSql

```advpl
Local cCodCli := "000001"
Local cFilNF  := "01"

BeginSql Alias "QRY"
    SELECT SF2.F2_DOC, SF2.F2_SERIE, SF2.F2_EMISSAO, SD2.D2_COD, SD2.D2_QUANT
      FROM %table:SF2% SF2
      JOIN %table:SD2% SD2
        ON SD2.D2_FILIAL = SF2.F2_FILIAL
       AND SD2.D2_DOC    = SF2.F2_DOC
       AND SD2.D2_SERIE  = SF2.F2_SERIE
       AND SD2.D2_CLIENTE= SF2.F2_CLIENTE
       AND SD2.%notDel%
     WHERE SF2.F2_FILIAL = %xfilial:SF2%
       AND SF2.F2_CLIENTE= %exp:cCodCli%
       AND SF2.%notDel%
     ORDER BY %Order:SF2%
EndSql

// Tipagem de colunas — recomendado (PERF-003)
TCSetField("QRY", "F2_EMISSAO", "D")
TCSetField("QRY", "D2_QUANT",   "N", 11, 2)

While QRY->(!Eof())
    // ...processa
    QRY->(DbSkip())
EndDo
QRY->(DbCloseArea())
```

## As 6 restrições de SQL embarcado (regras de lint)

| Regra      | Severidade | O que não fazer                                                          |
|------------|------------|--------------------------------------------------------------------------|
| `PERF-001` | critical   | `SELECT *` em BeginSql ou TCQuery — fetcha colunas inúteis, memo fields  |
| `PERF-002` | warning    | `DbSeek` dentro de `While`/`For` quando JOIN/IN resolveria               |
| `PERF-003` | warning    | Faltar `TCSetField` para datas/numéricos pós-query                       |
| `PERF-004` | warning    | Concatenação de string com `+`/`+=` em loop (use array + `Array2String`) |
| `PERF-005` | warning    | `RecCount()` para checar existência — use `!Eof()` (full scan)           |
| `PERF-006` | info       | `WHERE`/`ORDER BY` em colunas sem índice (consulte SIX)                  |
| `SEC-001`  | critical   | SQL com `+ cVar +` em vez de `%exp:cVar%` — SQL injection                |

## TCQuery (legacy) — quando aparece

```advpl
// Estilo antigo — funciona, mas sem macros
Local cSql := "SELECT F2_DOC FROM " + RetSqlName("SF2") + " "
cSql += "WHERE F2_FILIAL = '" + xFilial("SF2") + "' "
cSql += "  AND F2_CLIENTE = '" + cCodCli + "' "
cSql += "  AND D_E_L_E_T_ <> '*'"

TCQuery cSql NEW ALIAS "QRY"
```

**Migrate para `BeginSql`** sempre que possível. Se manter `TCQuery`, **NUNCA** concatene variável de input — use `%exp:` em string template do `BeginSql`.

## `RetSqlName`, `xFilial` (equivalentes ADVPL)

Quando precisar de SQL fora de `BeginSql` (ex: query dinâmica complexa):

- `RetSqlName("SA1")` ↔ `%table:SA1%` (retorna nome físico).
- `xFilial("SA1")` ↔ `%xfilial:SA1%` (retorna filial atual, sem aspas — precisa adicionar).
- `D_E_L_E_T_ <> '*'` ↔ `%notDel%`.

## Workflow recomendado

1. Antes de escrever SQL, rode `/plugadvpl:sql --op select --table SF2` para ver exemplos existentes no projeto.
2. Use sempre `BeginSql ... EndSql` para queries novas.
3. Liste **explicitamente** as colunas (`SELECT F2_DOC, F2_SERIE, ...`) — nunca `SELECT *`.
4. Adicione `%notDel%` em **toda** tabela ADVPL que aparecer no `FROM`/`JOIN`.
5. Adicione `%xfilial:TABLE%` no `WHERE` para filtro multi-filial.
6. Bind variáveis com `%exp:` — nunca concatene.
7. Após `EndSql`, chame `TCSetField` para `D` (data) e `N` (numérico).
8. Sempre `DbCloseArea()` no fim ou em `Recover`.

## Anti-padrões

```advpl
// SEC-001 — SQL injection
cSql := "SELECT * FROM SA1010 WHERE A1_COD = '" + cCod + "'"

// PERF-001 — SELECT *
BeginSql Alias "QRY"
    SELECT * FROM %table:SA1% SA1 WHERE SA1.%notDel%
EndSql

// PERF-002 — DbSeek em loop
While SC5->(!Eof())
    DbSelectArea("SA1")

From replybr/plugadvpl, MIT.

More from replybr/plugadvpl

Signals

Stars
0
Forks
5
Open issues
0
Last commit (this folder)
May 11, 2026 (5 months ago)

From GitHub (fetched Oct 24, 2026).

Details

License
MIT
First seen
Oct 24, 2026
Last checked
Oct 24, 2026

More from replybr

Done for you

Rather not set it up yourself?

Chalk Labs scopes, builds and runs agents for your team: the task audit, the eval harness, the integrations and the weekly ops report. Fixed scope, priced before we start.

See how we build AI agents →Read the case studies

Chalk Labs is a two-founder AI and growth studio. We build AI agents, CRMs and MVPs for Web3 and AI companies, and have shipped 50+ product and token launches.

launches shipped
50+
launches shipped
media placements
500+
media placements
founders on every account
2
founders on every account

Press we've placed: Forbes · CoinDesk · Decrypt · Cointelegraph · The Block

Rahil Jain, Co-founder, Chalk Labs. Ex-Goldman Sachs engineer. You talk to the person who builds it.

Get advpl-embedded-sql running in your business

Tell us what you want to automate. A founder replies within one working day with a plan, not a pitch deck.

Goes to the Chalk Labs founders, not the newsletter. Privacy

Need help deploying advpl-embedded-sql?

CallGet help
Cite this page

advpl-embedded-sql skill. Agent Mart. https://agentmart.dev/skills/replybr-advpl-embedded-sql (last updated 2026-10-24)