---
name: Embedded SQL
slug: embedded-sql
category: AI Engineering
description: Embedded SQL helps write, review, and migrate SQL in ADVPL/TLPP using BeginSQL/EndSQL blocks, macro expressions, joins, aggregations, subqueries, and TCSqlExec. Use it when converting TCQuery code or building safer queries with filial and deletion filters.
github: "https://github.com/thalysjuvenal/advpl-specialist/tree/main/skills/embedded-sql"
language: Shell
stars: 186
forks: 37
install: "npx degit https://github.com/thalysjuvenal/advpl-specialist/tree/main/skills/embedded-sql ~/.claude/skills/embedded-sql"
installs_to: ~/.claude/skills/embedded-sql
source_path: skills/embedded-sql/SKILL.md
collection_size: 18
category_size: 3278
collection_url: "https://dirskills.com/collections/thalysjuvenal/advpl-specialist"
added: 2026-09-06T05:20:28.728Z
last_synced: 2026-09-06T05:20:28.728Z
canonical_url: "https://dirskills.com/skills/embedded-sql"
---

# Embedded SQL

Embedded SQL helps write, review, and migrate SQL in ADVPL/TLPP using BeginSQL/EndSQL blocks, macro expressions, joins, aggregations, subqueries, and TCSqlExec. Use it when converting TCQuery code or building safer queries with filial and deletion filters.

**Install:**

```bash
npx degit https://github.com/thalysjuvenal/advpl-specialist/tree/main/skills/embedded-sql ~/.claude/skills/embedded-sql
```

## README

# Embedded SQL in ADVPL/TLPP

This skill covers writing SQL inside ADVPL/TLPP using `BeginSQL ... EndSQL` blocks and their special macro expressions -- `%table:TABLE%` for physical table resolution, `%xfilial:TABLE%` for branch filtering, `%notDel%` for logical-deletion filtering, `%exp:EXPRESSION%` for safe variable binding, and `%Order:TABLE%` for primary-key ordering. It also covers `column` type declarations, JOIN and aggregation patterns, subqueries, and migrating legacy `TCQuery` string-concatenation code to Embedded SQL.

Activate this skill whenever the user is writing a new query, reviewing an existing one for correctness/safety, or converting old concatenated-string SQL to the modern BeginSQL syntax. It also covers the restrictions and gotchas of BeginSQL (no `*` at line start, always include `%notDel%` and filial filtering, use `GetNextAlias()`, close aliases) and when to use `TCSqlExec` instead for INSERT/UPDATE/DELETE.

It does not cover general native function lookups unrelated to SQL (see `protheus-reference`), business-process context for which tables/fields to query (see `protheus-business`), or generating documentation for a routine that happens to contain SQL (see `documentation-patterns`).

| Reference file | Read when |
|---|---|
| reference.md | Always -- BeginSQL syntax, macro expressions, JOIN/aggregation/subquery patterns, DML via TCSqlExec, performance tips, and TCQuery migration |
