Tempo de leitura: 2 minutos
Olá, no post de hoje iremos falar da função auxiliar GetLastQuery() dentro do desenvolvimento de querys no TOTVS Protheus.
Muitas vezes como forma de facilitar o desenvolvimento de querys no Protheus utilizamos o que chamamos de Embedded SQL, que são querys SQL embutidas no código ADVPL, no entanto quando vamos depurar um código ADVPL que tenha essas características não é possível colocar pontos de paradas entre as instruções BeginSQL e EndSQL, assim não conseguiremos obter a consulta SQL que foi montada dentro desses blocos de instruções.
Para conseguirmos obter a consulta SQL montada utilizamos a função GetLastQuery(), essa função obtém a última consulta SQL montada entre BeginSQL e EndSQL.
Após a abertura do cursor, no alias especificado, a função GetLastQuery() retorna um array, com 5 elementos, onde estão disponíveis as seguintes informações sobre a query executada:
[1] cAlias – Alias usado para abrir o cursor.
[2] cQuery – Query executada.
[3] aCampos – Array de campos com critério de conversão especificados.
[4] lNoParser – Caso verdadeiro (.T.), não foi utilizada a função ChangeQuery() na string original.
[5] nTimeSpend – Tempo, em segundos, utilizado para abertura do cursor.
#include 'protheus.ch'
#include "tbiconn.ch"
/*{Protheus.doc} User Function fGetLQry
(Essa função tem por finalidade mostrar a função GetLastQuery para retornar
a última consulta SQL executada entre BeginSQL e EndSQL)
@type Function
@author Adilio Costa
@since 23/06/2023
@version 12.1.2210
Acesse e se inscreva no Blog/Site ProtheusAdvpl para receber conteúdo:
www.protheusadvpl.com.br
Se inscreva no Canal do ProtheusAdvpl para receber vídeos:
https://wwww.youtube.com/protheusadvpl
Siga-nos no Instagram:
http://https://www.instagram.com/protheusadvpl
Siga a página do ProtheusAdvpl no Facebook:
https://www.facebook.com/protheusadvpl2
Conheça o Portal ProtheusAdvpl:
https://hotmart.com/pt-br/marketplace/produtos/portal-protheusadvpl/O10336806O
*/
//SQL no Protheus - Embedded SQL - fGetLQry
User Function fGetLQry()
Local cAlias := ""
Local cQuery := ""
Local cMsg := ""
//Abro o ambiente
RpcSetType(3)
PREPARE ENVIRONMENT EMPRESA "99" FILIAL "01" MODULO "COM"
//Construindo a consulta
BeginSQL Alias "SQL_SB1"
SELECT
B1_COD,
B1_DESC
FROM
%table:SB1% SB1
WHERE
B1_FILIAL = %xFilial:SB1%
AND SB1.%notDel%
EndSQL
//Fecha a consulta com DBCloseArea()
SQL_SB1->(DBCloseArea())
//Obtém as informações da última query executada entre BeginSQL e EndSQL
aDados := GetLastQuery()
cAlias := aDados[1]
cQuery := aDados[2]
//Mostra mensagem com todas as informações capturadas
cMsg += "* cAlias - " + cAlias + Chr(13) + Chr(10)
cMsg += "* cQuery - " + cQuery
MsgInfo(cMg, "Atenção")
Return
Na imagem abaixo podemos ver o retorno da função GetLastQuery() do exemplo do código ADVPL acima:
Bem, finalizamos mais um post aqui no Blog/Site ProtheusAdvpl, espero que você tenha entendido. Qualquer dúvida deixe seu comentário ou nos mande um e-mail para adiliocosta@protheusadvpl.com.br.
Links de referências:
- https://tdn.totvs.com/display/public/framework/Desenvolvendo+queries+no+Protheus
- https://terminaldeinformacao.com/knowledgebase/getlastquery/
Link permanente
Link permanente
I like the valuable information you supply in your articles. I will bookmark your weblog and check again here frequently. I am rather certain I will be told a lot of new stuff right here! Good luck for the following!
Link permanente
Thanks for sharing your thoughts on %meta_keyword%. Regards
Link permanente
Helpful info. Fortunate me I found your site accidentally, and I am stunned why this coincidence did not happened in advance! I bookmarked it.
Link permanente
Excellent post. I’m dealing with some of these issues as well..
Link permanente
Why users still use to read news papers when in this technological world all is available on net?
Link permanente
Hi my loved one! I want to say that this article is awesome, great written and come with almost all significant infos. I’d like to peer more posts like this .
Link permanente
This is very interesting, You are an overly professional blogger. I have joined your feed and look ahead to looking for more of your magnificent post. Also, I have shared your web site in my social networks
Link permanente
I am sure this piece of writing has touched all the internet users, its really really good piece of writing on building up new blog.
Link permanente
fantastic issues altogether, you just won a logo new reader. What might you suggest in regards to your submit that you simply made a few days ago? Any sure?
Link permanente
Appreciating the hard work you put into your site and in depth information you provide. It’s great to come across a blog every once in a while that isn’t the same outdated rehashed material. Wonderful read! I’ve saved your site and I’m including your RSS feeds to my Google account.
Link permanente
My coder is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using Movable-type on numerous websites for about a year and am anxious about switching to another platform. I have heard very good things about blogengine.net. Is there a way I can transfer all my wordpress content into it? Any kind of help would be really appreciated!
Link permanente
Keep on working, great job!
Link permanente
What’s up, its nice article about media print, we all understand media is a wonderful source of information.
Link permanente
A person necessarily lend a hand to make significantly articles I would state. This is the first time I frequented your web page and thus far? I amazed with the research you made to create this actual submit amazing. Fantastic task!
Link permanente
I am curious to find out what blog system you have been working with? I’m experiencing some minor security problems with my latest site and I would like to find something more safe. Do you have any solutions?
Link permanente
Magnificent web site. Lots of useful information here. I’m sending it to some pals ans also sharing in delicious. And of course, thank you on your effort!
Link permanente
It’s impressive that you are getting ideas from this article as well as from our argument made here.