ChangeQuery and database compatibility
Adapts query syntax before opening it through DBAccess.
Overview
ChangeQuery(cQuery) returns SQL text adapted to the current database without opening a cursor or executing the query. Documentation requires ANSI syntax, recommends WHERE and describes parser limits: it does not guarantee compatibility for every database-native function. The community TCGenQry.PRW transforms text before TCGenQry/DbUseArea; that example does not explicitly constrain branch, so it must not be copied as an access policy. The editorial example uses a fictional table and only measures returned text. For actual queries, review RetSqlName, branch and logical-deletion filters and bound parameters. (TOTVS, n.d., ref. 1; EURAI, n.d., ref. 2)
Commented example
#Include "Protheus.ch"
User Function Ed299()
Local cSql := "SELECT CODIGO FROM LIVROS WHERE 1 = 1"
Local cAdapted
// Fictional table; no query is opened.
cAdapted := ChangeQuery(cSql)
ConOut(cValToChar(Len(cAdapted)))
Return NilLength depends on parser and database; there is no record set or fixed numeric result. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.
- Do not use ChangeQuery as input sanitization or authorization. Test the query and plan on supported target databases.
- Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Comments
There are no approved comments yet.
Sign in with Google or Microsoft to comment.