Fórum Select nao funciona no delphi #454391
06/09/2013
0
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
Curtir tópico
+ 0Posts
06/09/2013
Gustavo Bretas
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
06/09/2013
Deivison Melo
Gostei + 0
09/09/2013
Wilton Júnior
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
09/09/2013
Wilton Júnior
"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
09/09/2013
Gustavo Bretas
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
09/09/2013
Wilton Júnior
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
09/09/2013
Wilton Júnior
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
09/09/2013
Gustavo Bretas
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
09/09/2013
Wilton Júnior
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
Clique aqui para fazer login e interagir na Comunidade :)