Select nao funciona no delphi

Delphi

06/09/2013

select a.codven, c.CODGRU,
sum((b.qtdven - b.qtddev) * b.valliq) as qtdven
from ftd001 a, ftd002 b, arq027 c
where a.tippag<=4
and month(a.datped)=01
and year(a.datped)=2013
and b.numped = a.numped
and c.tippro = b.tippro
and c.codpro = b.codpro
and (exists (select c.codgru from Arq200 where codgru = c.codgru) or
exists (select c.CODGRU from arq200 where c.codsub = CODSUB) or
exists (select c.codgru from Arq200 where c.CODTIP = codtip))
group by a.codven, c.codgru

O select assima funciona bem no sqlserver mas no delphi nao funcionar da erro de sintaxe.
Aguem pode ajudar

obrigado
"Deus Abençoe"
Wilton Júnior

Wilton Júnior

Curtidas 0

Respostas

Gustavo Bretas

Gustavo Bretas

06/09/2013

E ae Wilson, tranquilo?

Cara, testa assim:

select a.codven, 
       c.codgru, 
       sum((b.qtdven - b.qtddev) * b.valliq) as qtdven
  from ftd001 a, 
       ftd002 b, 
       arq027 c
 where a.tippag <= 4
   and month(a.datped) = 01
   and year(a.datped) = 2013
   and b.numped = a.numped
   and c.tippro = b.tippro
   and c.codpro = b.codpro
   and (exists (select null from arq200 where arq200.codgru = c.codgru) 
       or exists (select null from arq200 where arq200.codsub = c.codsub) 
       or exists (select null from arq200 where arq200.codtip = c.codtip))
 group by a.codven, c.codgru


Abraço!
GOSTEI 0
Deivison Melo

Deivison Melo

06/09/2013

Poste o código já no delphi!

GOSTEI 0
Wilton Júnior

Wilton Júnior

06/09/2013

//Levantando as Vendas Relizadas.
with ftd002 do
begin
close;
sql.clear;
if arq998tipobj.value = 1 then
sql.add('select a.codven, c.codgru,');
if arq998tipobj.value = 2 then
sql.add('select a.codven, c.codsub as codgru,');
if arq998tipobj.value = 3 then
sql.add('select a.codven, c.codtip as codgru,');
if arq998tipobj.value = 4 then
sql.add('select a.codven, c.codpro as codgru,');
if radiogroup2.itemindex = 0 then
sql.add('sum((b.qtdven - b.qtddev) * b.valliq) as qtdven')
else
sql.add('sum(b.qtdven - b.qtddev) as qtdven');

sql.add('from ftd001 a, ftd002 b, arq027 c');
sql.add('where a.tippag<=:wtippag');
parambyname('wtippag').value := 4;
if arq998gerobj.value = 0 then
begin
sql.add('and month(a.datped)=:wmesfec');
sql.add('and year(a.datped)=:wanofec');
end;
if arq998gerobj.value = 1 then
begin
sql.add('and month(a.datfat)=:wmesfec');
sql.add('and year(a.datfat)=:wanofec');
end;
if arq998gerobj.value = 2 then
begin
sql.add('and month(a.datfec)=:wmesfec');
sql.add('and year(a.datfec)=:wanofec');
end;
parambyname('wmesfec').value := wmesbas;
parambyname('wanofec').value := wanobas;
sql.add('and b.numped = a.numped');
sql.add('and c.tippro = b.tippro');
sql.add('and c.codpro = b.codpro');
{inicio wilton}
if arq998tipobj.value = 5 then
begin
sql.add('and exists (select c.codgru from Arq200 where codgru = c.codgru)');
sql.add('or exists (select c.CODGRU from arq200 where c.codsub = CODSUB)');
sql.add('or exists (select c.codgru from Arq200 where c.CODTIP = codtip)');
end;
{fim wilton}
if ((arq998tipobj.value = 1) or (arq998tipobj.value = 5)) then //wilton
sql.add('group by a.codven, c.codgru');
if arq998tipobj.value = 2 then
sql.add('group by a.codven, c.codsub');
if arq998tipobj.value = 3 then
sql.add('group by a.codven, c.codtip');
if arq998tipobj.value = 4 then
sql.add('group by a.codven, c.codpro');
open;
end;
GOSTEI 0
Wilton Júnior

Wilton Júnior

06/09/2013

Ola Bretas, esta funcionando mas continua aparecendo a janela de erro:

"Incorrect syntax near the keyword 'group'."
Incorrect syntax near the keyword 'or'.
Incorrect syntax near the keyword 'or'.
Incorrect syntax near 'b'.

obrigado
GOSTEI 0
Gustavo Bretas

Gustavo Bretas

06/09/2013

Wilton, vc precisa ser mais específico de quando ocorre o erro, seu select te muitas possibilidades!

Seu componente dataset esta no formulário/dm ou é criado no código?
Eu vejo que vc coloca os filtros de mês/ano somente se a opção foi 0, 1 e 2, mas seta o parâmetro independente disso.
O subselect que te passei pra usar no filtro, coloque todo o bloco entre parentese, senão os "OR" vão interferir nos outros filtros.

Que banco vc usa? SQL Server tem o Profile, e Oracle tem o Oracle Monitor que mostram o comando que vc enviou para o banco, no Firebird quando eu preciso ver o SQL montado, eu salvo em TXT pra testar!

ftd002.SQL.SaveToFile('ftd002.txt');


Faça os testes ae, depois nos diga se deu certo!

Abraço!
GOSTEI 0
Wilton Júnior

Wilton Júnior

06/09/2013

Ola Bretas, estou usando query local utilizo o msdac.
Antes de passar o select para o programa eu testo no sqlserver 2008 r2 onde la esta rodando perfeitamente.
O que to tendo dificuldade é de entender se no sqlserver roda pq no programa nao roda.
GOSTEI 0
Wilton Júnior

Wilton Júnior

06/09/2013

with ftd002 do
begin
close;
sql.clear;
if arq998tipobj.value = 1 then
sql.add('select a.codven, c.codgru,');
if arq998tipobj.value = 2 then
sql.add('select a.codven, c.codsub as codgru,');
if arq998tipobj.value = 3 then
sql.add('select a.codven, c.codtip as codgru,');
if arq998tipobj.value = 4 then
sql.add('select a.codven, c.codpro as codgru,');
if radiogroup2.itemindex = 0 then
sql.add('sum((b.qtdven - b.qtddev) * b.valliq) as qtdven')
else
sql.add('sum(b.qtdven - b.qtddev) as qtdven');

sql.add('from ftd001 a, ftd002 b, arq027 c');
sql.add('where a.tippag<=:wtippag');
parambyname('wtippag').value := 4;
if arq998gerobj.value = 0 then
begin
sql.add('and month(a.datped)=:wmesfec');
sql.add('and year(a.datped)=:wanofec');
end;
if arq998gerobj.value = 1 then
begin
sql.add('and month(a.datfat)=:wmesfec');
sql.add('and year(a.datfat)=:wanofec');
end;
if arq998gerobj.value = 2 then
begin
sql.add('and month(a.datfec)=:wmesfec');
sql.add('and year(a.datfec)=:wanofec');
end;
parambyname('wmesfec').value := wmesbas;
parambyname('wanofec').value := wanobas;
sql.add('and b.numped = a.numped');
sql.add('and c.tippro = b.tippro');
sql.add('and c.codpro = b.codpro');
{inicio wilton}
if arq998tipobj.value = 5 then
begin
sql.add('and (exists (select null from Arq200 where Arq200.codgru = c.codgru)');
sql.add('or exists (select null from arq200 where Arq200.CODSUB = c.codsub)');
sql.add('or exists (select null from Arq200 where Arq200.codtip = c.codtip))');
end;
{fim wilton}
if ((arq998tipobj.value = 1) or (arq998tipobj.value = 5)) then //wilton
sql.add('group by a.codven, c.codgru');
if arq998tipobj.value = 2 then
sql.add('group by a.codven, c.codsub');
if arq998tipobj.value = 3 then
sql.add('group by a.codven, c.codtip');
if arq998tipobj.value = 4 then
sql.add('group by a.codven, c.codpro');
open;
end;
GOSTEI 0
Gustavo Bretas

Gustavo Bretas

06/09/2013

Wilton, a questão é a variação que pode acontecer de acordo com seus IF´s!
Vc tem que testar o select depois que passar por todos os IF´s, e não antes, entende?
Se antes funciona, e depois não, alguma coisa esta errada nos IF´s, concorda?

Tem que debugar, é chato, mas não vejo outra forma!
GOSTEI 0
Wilton Júnior

Wilton Júnior

06/09/2013

Bretas achei a besteira que fiz.
Como vc disse tinha um problema nos if sim.
Eu o retardado esqueceu de colocar que na primeira linha checasse se o tipobj fosse 5 ou 1 igual fiz no linha do group by.
obrigado pela dica e Deus te Abençoe.
GOSTEI 0
POSTAR