douglasrs89
10 years agoNew Contributor
How to run a .sql file querys
Hi, I have a .sql file with many querys. This queries are written without following a standard format. I need a way to run this file without having to modify it.
The only way I could was reading letter by letter and each query rewriting in a row. However this made the process very slow.
What a way to run a file that contains formatting queries independent of them?
Here is my code.
function Auditoria(arquivoDiretorio: string): OleVariant;
arquivoTxt := aqFile.OpenTextFile(arquivoDiretorio, aqFile.faRead, aqFile.ctANSI);
quantidadeLinhas := arquivoTxt.LinesCount;
//HERE I READ THE FILE
.
.
.
arrQuery := CreateVariantArray(0, quantidadeLinhas);
posicaoArray := 0;
while not arquivoTxt.IsEndOfFile() do
begin
//HERE I REWRITE ALL QUERYS OF FILE.
.
.
.
end;
posicaoArray := posicaoArray - 1;
VarArrayRedim(arrQuery, posicaoArray);
//HERE ARE MY NEW QUERYS, EACH IN A LINE
result := arrQuery;
end;
function AuditoriaBanco(strBanco: string, arrQuery: OleVariant): boolean;
var
strQtdErro, strCodErro, strQuery: string;
oleQuery, oleConexao: OleVariant;
intContador, totalLinhas: integer;
begin
result := true;
oleConexao := FG_ConectaBanco(strBanco);
totalLinhas := VarArrayHighBound(arrQuery, 1);
for intContador := 0 to totalLinhas do
begin
strQuery := aqConvert.VarToStr(arrQuery[intContador]);
if strQuery <> '' then
begin
try
//HERE I EXECUTE THE QUERY
oleQuery := oleConexao.Execute_(strQuery);
except
Log.Warning('Ocorreu o Erro: ' + '"' + exceptionmessage + '"' + ' Ao executar a query: ' + strQuery);
end;
if (intContador = totalLinhas) then
begin
oleQuery := oleConexao.Execute_(strQuery);
oleQuery.MoveFirst;
while not aqConvert.VarToBool(oleQuery.EOF) do
begin
strQtdErro := aqConvert.VarToStr(oleQuery.Fields('QTD_ERROS').Value);
strCodErro := aqConvert.VarToStr(oleQuery.Fields('CODIGO').Value);
if strQtdErro > 0 then
begin
Log.Warning('A auditoria ' + strCodErro + ' retornou ' + strQtdErro + ' erro(s).');
result := false;
end;
oleQuery.MoveNext;
end;
end;
end;
end;
oleConexao.close;
end;