Como criar SQL Inserts a partir de uma Query no Delphi

Delphi

08/09/2005

Bom dia,


Estou criando um módulo de sistema onde o usuário executa uma query com dados de um Database. Eu preciso fazer com que os dados selecionados sejam inseridos em outra tabela com a mesma estrutura só que em outro database. Acontece que são muitas tabelas com muitos campos, o que tornaria trabalhoso conhecer todos os campos das tabelas, selecionar os dados da query gerada no DataBase1 e gravar no Database2.
Gostaria de saber se alguem conhece alguma forma de criar SQl Inserts a partir de uma Query gerada pela aplicação.

grato

Obs. Delphi 6 + Oracle 9i usando BDE.


Outl@w

Outl@w

Curtidas 0

Respostas

Outl@w

Outl@w

08/09/2005

sobe


GOSTEI 0
Outl@w

Outl@w

08/09/2005

subindo


GOSTEI 0
Xanatos

Xanatos

08/09/2005

EU fiz uma assim... mas só para consultas com uma tabela...

function RetornaNomeTabela(Sql: string):string;
var
  i: integer;
  NomeTab: string;
  PosFrom: integer;
  Str: string[1];
begin
  Nometab:= ´´;
  PosFrom:= Pos(´FROM´,UpperCase(Sql));
  Str:= ´ ´;
  for i:=PosFrom+4 to length(Sql) do
  begin
    if (Sql[i] <> ´ ´) then
    begin
      Nometab:= Nometab+ Sql[i];
      Str:= ´.´;
    end
    else if Str= ´.´ then
      break;
  end;
  RetornaNomeTabela:= NomeTab;
end;

procedure TfrmGeraSql.sbSqlClick(Sender: TObject);
var
  i: integer;
  CaminhoArq: string;
  Arq_Saida: TStringList;
  VetCampos: array [1..50] of string;
  ContCampos: integer;
  Valores: string;

begin
  Arq_Saida:= TStringList.Create;

  SaveDialog1.InitialDir:= ExtractFilePath(Application.ExeName);
  SaveDialog1.Execute;
  CaminhoArq:= SaveDialog1.FileName;


  pbSql.Min:= 0;
  pbSql.Max:= Query1.RecordCount;
  pbSql.Position:= 0;

  ContCampos:= DBGrid1.Columns.Count;
  for i:= 0 to DBGrid1.Columns.Count-1 do
  begin
    VetCampos[i+1]:= DBGrid1.Columns.Items[i].FieldName;
  end;

  Query1.First;


  while not Query1.Eof do
  begin
    pbSql.Position:= pbSql.Position+1;

    Valores:= ´(´;
    for i:= 1 to ContCampos do
    begin
      if (Query1.Fields[i-1].DataType = ftString) then
        Valores:= Valores +  ´´´´+  Query1.Fields[i-1].AsString + ´´´´
      else
        Valores:= Valores +  Query1.Fields[i-1].AsString;
      if i+1 <= ContCampos then
        Valores:= Valores + ´,´;
    end;
    Valores:= Valores + ´)´;

    Arq_Saida.Add(´Insert into ´+RetornaNomeTabela(SqlExec)+ ´ Values ´+ Valores);


    query1.Next;
  end;
  Arq_Saida.SaveToFile(CaminhoArq);

  Arq_Saida.Free;
end;




GOSTEI 0
Outl@w

Outl@w

08/09/2005

ok vou testar aqui!

thanks


GOSTEI 0
Outl@w

Outl@w

08/09/2005

Show de bola!

Funcionou direitinho! Valeu!


GOSTEI 0
Xanatos

Xanatos

08/09/2005

Blz bicho...precisei por acaso esses dias ... boa sorte ai....


GOSTEI 0
POSTAR