Problemas ADOQuery
Boa Tarde,
Estou desenvolvendo uma aplicação e estou tendo dificuldades para executar um bloco com instruções SQL, alguém sabe me dizer onde está errado?
Segue bloco abaixo:
Begin
if CBSearch.Items[CBSearch.ItemIndex] = 'Operador' then
begin
Consulta1:= EditSearch.Text;
with DM.ADOQueryMANST do
begin
Close;
SQL.Clear;
SQL.Add('declare @tbTeste table (tempototal varchar(14))');
SQL.Add('insert into @tbTeste select M.TempTotal from TB_MAN M');
SQL.Add('WHERE (Mec1 = ' + QuotedStr(Consulta1));
SQL.Add(' OR Mec2 = ' +QuotedStr(Consulta1));
SQL.Add('OR Mec3 = ' + QuotedStr(Consulta1)+ ' OR Mec4 = ' + QuotedStr(Consulta1));
SQL.Add('OR Mec5 = ' + QuotedStr(Consulta1)+ ' OR Mec6 = ' + QuotedStr(Consulta1));
SQL.Add('OR Mec7 = ' + QuotedStr(Consulta1)+ ' OR Mec8 = ' + QuotedStr(Consulta1));
SQL.Add('OR Mec9 = ' + QuotedStr(Consulta1)+ ') ' );
SQL.Add(';with cte_dados as');
SQL.Add('(select');
SQL.Add('TabHora.ColXML.value('@Ind', 'int') AS Hora,');
SQL.Add('TabMin.ColXML.value('@Ind', 'int') AS Minuto,');
SQL.Add(' TabSeg.ColXML.value('@Ind', 'int') AS Segundo');
SQL.Add('FROM (SELECT *,');
SQL.Add('CONVERT(XML, '<Test Ind="' + Replace(tempototal, ':','"/><Test Ind="') + '"/>') AS ColXML');
SQL.Add('FROM @tbTeste) AS Tab');
SQL.Add('CROSS APPLY Tab.ColXML.nodes('/Test[1]') As TabHora (ColXML)');
SQL.Add('CROSS APPLY Tab.ColXML.nodes('/Test[2]') As TabMin (ColXML)');
SQL.Add('CROSS APPLY Tab.ColXML.nodes('/Test[3]') As TabSeg (ColXML)');
SQL.Add('), cte_soma as(');
SQL.Add('select SUM(hora) 'hora' , SUM(minuto) 'minuto' , SUM(segundo) 'segundo' from ');
SQL.Add('cte_dados)');
SQL.Add('select hora+(minuto/60)as 'hora', case When minuto>59 then minuto%60 else ');
SQL.Add('minuto end as 'minuto',segundo from cte_soma');
Open;
Edit1.Text:= DM.AdoQueryMANST.Fieldbyname('Hor H/Maq').AsString + ':' + Formatfloat('00',StrtoInt(DM.AdoQueryMANST.Fieldbyname('Min H/Maq').AsString));
end;
end; end; OBS: Uso o RAD Studio 2010 Att, Moacir Rachid
if CBSearch.Items[CBSearch.ItemIndex] = 'Operador' then
begin
Consulta1:= EditSearch.Text;
with DM.ADOQueryMANST do
begin
Close;
SQL.Clear;
SQL.Add('declare @tbTeste table (tempototal varchar(14))');
SQL.Add('insert into @tbTeste select M.TempTotal from TB_MAN M');
SQL.Add('WHERE (Mec1 = ' + QuotedStr(Consulta1));
SQL.Add(' OR Mec2 = ' +QuotedStr(Consulta1));
SQL.Add('OR Mec3 = ' + QuotedStr(Consulta1)+ ' OR Mec4 = ' + QuotedStr(Consulta1));
SQL.Add('OR Mec5 = ' + QuotedStr(Consulta1)+ ' OR Mec6 = ' + QuotedStr(Consulta1));
SQL.Add('OR Mec7 = ' + QuotedStr(Consulta1)+ ' OR Mec8 = ' + QuotedStr(Consulta1));
SQL.Add('OR Mec9 = ' + QuotedStr(Consulta1)+ ') ' );
SQL.Add(';with cte_dados as');
SQL.Add('(select');
SQL.Add('TabHora.ColXML.value('@Ind', 'int') AS Hora,');
SQL.Add('TabMin.ColXML.value('@Ind', 'int') AS Minuto,');
SQL.Add(' TabSeg.ColXML.value('@Ind', 'int') AS Segundo');
SQL.Add('FROM (SELECT *,');
SQL.Add('CONVERT(XML, '<Test Ind="' + Replace(tempototal, ':','"/><Test Ind="') + '"/>') AS ColXML');
SQL.Add('FROM @tbTeste) AS Tab');
SQL.Add('CROSS APPLY Tab.ColXML.nodes('/Test[1]') As TabHora (ColXML)');
SQL.Add('CROSS APPLY Tab.ColXML.nodes('/Test[2]') As TabMin (ColXML)');
SQL.Add('CROSS APPLY Tab.ColXML.nodes('/Test[3]') As TabSeg (ColXML)');
SQL.Add('), cte_soma as(');
SQL.Add('select SUM(hora) 'hora' , SUM(minuto) 'minuto' , SUM(segundo) 'segundo' from ');
SQL.Add('cte_dados)');
SQL.Add('select hora+(minuto/60)as 'hora', case When minuto>59 then minuto%60 else ');
SQL.Add('minuto end as 'minuto',segundo from cte_soma');
Open;
Edit1.Text:= DM.AdoQueryMANST.Fieldbyname('Hor H/Maq').AsString + ':' + Formatfloat('00',StrtoInt(DM.AdoQueryMANST.Fieldbyname('Min H/Maq').AsString));
end;
end; end; OBS: Uso o RAD Studio 2010 Att, Moacir Rachid
Moacir Rachid
Curtidas 0
Respostas
Rafael Mattos
10/05/2011
mas que erro está dando
GOSTEI 0
Moacir Rachid
10/05/2011
Segue abaixo a lista de erros do Delphi:
Build
Checking project dependencies...
Compiling PSYSQUALY.dproj (Debug configuration)
[DCC Error] UGerNC.pas(279): E2029 ')' expected but '@' found
[DCC Error] UGerNC.pas(283): E2038 Illegal character in input file: '"' (#$22)
[DCC Error] UGerNC.pas(283): E2038 Illegal character in input file: '"' (#$22)
[DCC Error] UGerNC.pas(283): E2038 Illegal character in input file: '"' (#$22)
[DCC Error] UGerNC.pas(303): E2029 '.' expected but ';' found
[DCC Hint] UGerNC.pas(33): H2219 Private symbol 'FullTimeToStr' declared but never used
[DCC Hint] UGerNC.pas(34): H2219 Private symbol 'TOTALIZATEMPO' declared but never used
[DCC Fatal Error] PSYSQUALY.dpr(31): F2063 Could not compile used unit 'UGerNC.pas'
Failed
Elapsed time: 00:00:00.7
Output
Build started 10/05/2011 16:48:17.
Checking project dependencies...
Compiling PSYSQUALY.dproj (Debug configuration)
[DCC Error] UGerNC.pas(279): E2029 ')' expected but '@' found
[DCC Error] UGerNC.pas(283): E2038 Illegal character in input file: '"' (#$22)
[DCC Error] UGerNC.pas(283): E2038 Illegal character in input file: '"' (#$22)
[DCC Error] UGerNC.pas(283): E2038 Illegal character in input file: '"' (#$22)
[DCC Error] UGerNC.pas(303): E2029 '.' expected but ';' found
[DCC Hint] UGerNC.pas(33): H2219 Private symbol 'FullTimeToStr' declared but never used
[DCC Hint] UGerNC.pas(34): H2219 Private symbol 'TOTALIZATEMPO' declared but never used
[DCC Fatal Error] PSYSQUALY.dpr(31): F2063 Could not compile used unit 'UGerNC.pas'
Failed
Elapsed time: 00:00:00.7
Output
Build started 10/05/2011 16:48:17.
GOSTEI 0
Rodrigo Mattos
10/05/2011
Você está usando o firebird?
GOSTEI 0
Rafael Mattos
10/05/2011
SQL.Add('TabHora.ColXML.value('@Ind', 'int') AS Hora,'); <-- @IND ISSO NÃO DEVERIA TA DENTRO DA STRING?
SQL.Add('TabMin.ColXML.value('@Ind', 'int') AS Minuto,'); <-- @IND ISSO NÃO DEVERIA TA DENTRO DA STRING?
SQL.Add(' TabSeg.ColXML.value('@Ind', 'int') AS Segundo'); <-- @IND ISSO NÃO DEVERIA TA DENTRO DA STRING?
A MESMA COISA AQUI
SQL.Add('CONVERT(XML, '<Test Ind="' + Replace(tempototal, ':','"/><Test Ind="') + '"/>') AS ColXML');
GOSTEI 0
Moacir Rachid
10/05/2011
Uso o SQL Server 2005, no SQL esse script funciona corretamento, pois meu campo é do tipo varchar e esse código converte para TIME. Meu problema é a conversão dentro do Delphi, deve haver algum tipo de variável que execute a conversão para o Delphi, só não sei como fazer isso.
Exemplo do código abaixo que usa código do Delphi para interpretação:
SQL.Add('select * from TB_MAN WHERE TB_MAN.Maquina = ' + QuotedStr( Consulta)+ ' ORDER BY TB_MAN.COD');
QuoteStr é uma variável de conversão.
GOSTEI 0
Rafael Mattos
10/05/2011
SQL.Add('CONVERT(XML, '<Test Ind="' + Replace(tempototal, ':','"/><Test Ind="') + '"/>') AS ColXML');
<Test Ind=
isso ta fora da String deveria ser dentro da String
<Test Ind=
isso ta fora da String deveria ser dentro da String
GOSTEI 0
Moacir Rachid
10/05/2011
Como que é o correto?
No SQL tá assim:
declare @tbTeste table (tempototal varchar(14))
insert into @tbTeste select M.TempTotal from TB_MAN M
WHERE M.OS LIKE '%10%' ;with cte_dados as
(
select TabHora.ColXML.value('@Ind', 'int') AS Hora,
TabMin.ColXML.value('@Ind', 'int') AS Minuto,
TabSeg.ColXML.value('@Ind', 'int') AS Segundo FROM (SELECT *,
CONVERT(XML, '<Test Ind="' + Replace(tempototal, ':','"/><Test Ind="') + '"/>') AS ColXML
FROM @tbTeste) AS Tab
CROSS APPLY Tab.ColXML.nodes('/Test[1]') As TabHora (ColXML)
CROSS APPLY Tab.ColXML.nodes('/Test[2]') As TabMin (ColXML)
CROSS APPLY Tab.ColXML.nodes('/Test[3]') As TabSeg (ColXML)
), cte_soma as(
select SUM(hora) 'hora' , SUM(minuto) 'minuto' , SUM(segundo) 'segundo' from cte_dados) select hora+(minuto/60)as 'hora', case When minuto>59 then minuto%60 else minuto end as 'minuto',segundo from cte_soma
insert into @tbTeste select M.TempTotal from TB_MAN M
WHERE M.OS LIKE '%10%' ;with cte_dados as
(
select TabHora.ColXML.value('@Ind', 'int') AS Hora,
TabMin.ColXML.value('@Ind', 'int') AS Minuto,
TabSeg.ColXML.value('@Ind', 'int') AS Segundo FROM (SELECT *,
CONVERT(XML, '<Test Ind="' + Replace(tempototal, ':','"/><Test Ind="') + '"/>') AS ColXML
FROM @tbTeste) AS Tab
CROSS APPLY Tab.ColXML.nodes('/Test[1]') As TabHora (ColXML)
CROSS APPLY Tab.ColXML.nodes('/Test[2]') As TabMin (ColXML)
CROSS APPLY Tab.ColXML.nodes('/Test[3]') As TabSeg (ColXML)
), cte_soma as(
select SUM(hora) 'hora' , SUM(minuto) 'minuto' , SUM(segundo) 'segundo' from cte_dados) select hora+(minuto/60)as 'hora', case When minuto>59 then minuto%60 else minuto end as 'minuto',segundo from cte_soma
GOSTEI 0
Moacir Rachid
10/05/2011
Ninguém tem a solução?
GOSTEI 0
Wilson Junior
10/05/2011
Ao invés de chamar ADOQuery.Open, chame ADOQuery.ExecSQL.
Espero ter colaborado.
Espero ter colaborado.
GOSTEI 0
Moacir Rachid
10/05/2011
O problema está na conversão do SQL.Add('TabHora.ColXML.value('@Ind', 'int') AS Hora,');
O Delphi não entende isso!?!
GOSTEI 0