Usina BRAdvPL Guide
Sign in
← All topics
PRACTICAL GUIDEUnder review

TCSqlExec and SQL statement execution

TCSqlExec(cStatement) -> nStatus

Executes one SQL statement on the current DBAccess connection.

TCSqlExec and SQL statement executionAdvPL
01 · OVERVIEW

Overview

TCSqlExec returns a negative numeric status on failure; do not treat it as affected-row count. Statements depend on the DBMS. TDN warns that direct SQL does not update DBAccess control fields. The community example updates a custom product field; this page analyzes the pattern without reproducing a master-data update. The uninvoked block only prepares execution. Connection/transaction owners must define SQL and error handling. (Sources: refs. 1–2.)

02 · SYNTAX

Syntax

TCSqlExec(cStatement) -> nStatus

Parameters

cStatement
CharacterRequired

SQL statement for the authorized DBMS/context.

Return value

Numeric; negative indicates failure.

03 · PRACTICAL EXAMPLE

Commented example

#Include "Protheus.ch"

User Function Ed688()
    // Do not invoke with external SQL: define a statement in an isolated lab.
    Local bExecute := {|cStatement| TCSqlExec(cStatement)}
    ConOut(ValType(bExecute))
Return Nil
Expected result

Creates block B without SQL execution. During integration check status < 0 and retrieve TCSQLError in the failure context. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

BEST PRACTICES
  • Use one statement per call; neither concatenate external input nor bypass ERP business rules.
  • Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Related content

REFERENCES
  1. TOTVS. TCSQLExec. Retorno e campos de controle. Accessed on 2026-10-28.
  2. Atilio. dan-atilio/AdvPL. Maratona de Exemplos/Exemplo_482_Funcoes_TCSQLExec_e_TCSQLError.prw. Arquivo completo consultado; adaptação editorial. Accessed on 2026-10-28.
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