Fórum Atulizar campos em outra tabela - for ? #131588
04/04/2010
0
begin
cdsOrigem.first;
while not cdsOrigem.eof do
begin
while not cdsDestino.eof do
begin
if cdsDestinoCPF.AsString = cdsOrigemCPF.AsString then
Begin
cdsDestino.Edit; cdsDestinoDDD_RES.AsString := cdsOrigemDDD .AsString;
cdsDestinoTEL_RES.AsString := cdsOrigemTELEFONE.AsString; cdsDestino.Post;
cdsDestino.ApplyUpdates(0); cdsDestino.next;
End;
cdsOrigem.next;
end;
end;
end; No aguardo... Abraços.
Osvaldo Domênico
Curtir tópico
+ 0Posts
04/04/2010
Emerson Nascimento
Gostei + 0
04/04/2010
Osvaldo Domênico
update clientes cli set cli.ddd_res =:DDD_RES, cli.tel_res =:TEL_RES where (exists(select tel.ddd_1, tel.tel_1 from telefones tel where cli.cpf = tel.cpf))
Ainda esta um pouco confuso para eu entender, estou usando o IBEXPERT em um banco de dados FIREBIRD, coloquei os dois parametros e executei, logicamente ele pede o 1. e 2. parametro para preencher, é ai que eu estou me perdendo porque eu quero que ele verifique o CPF se existir ele pega o campo da tabela origem e copia na destino, fiz um teste colocando por exemplo o valor no 1. params de 12 e 2. de 0000-0000, ele executa e confirma a quantidade de registros atualizados porem vou fazer a consulta e não esta gravando, por enquanto estou usando só o console do ibexpert.
Se puder ajudar ficarei muito grato. Abraços
Gostei + 0
04/04/2010
Osvaldo Domênico
update clientes cli set cli.ddd_res =:DDD_RES, cli.tel_res =:TEL_RES where (exists(select tel.ddd_1, tel.tel_1 from telefones tel where cli.cpf = tel.cpf))
No IBEXPERT funciona perfeito só que forma manual e um a um. O que eu preciso é que ele faço a comparação e se existir faça o UPDATE, cheguei neste codigo, porem esta gravando os campos em branco na tabela destino, mesmo existindo os dados na tabela origem.
Begin
with sqlqryClientes do
begin
Close;
SQL.Clear;
SQL.Add('select DDD_RES,TEL_RES from clientes');
Open;
If not IsEmpty then
begin
first;
If not eof then
begin
close;
sql.clear;
sql.add ('update clientes cli set cli.ddd_res =:DDD_RES, ');
sql.add(' cli.tel_res =:TEL_RES ');
sql.add(' where (exists(select tel.ddd_1, tel.tel_1 ');
sql.add(' from telefones tel where cli.cpf = tel.cpf)) ');
Params[0].AsString := ClientDataSet1DDD_RES.AsString;
Params[1].AsString := ClientDataSet1TEL_RES.AsString;
Open;
end;
Next;
end;
exit;
end;
end;
Se puder ajudar ficarei muito grato. Abraços
Gostei + 0
06/04/2010
Emerson Nascimento
procedure TForm2.BitBtn1Click(Sender: TObject);
begin
cdsOrigem.first;
cdsDestino.Close;
cdsDestino.CommandText :=
'update clientes set '+
' ddd_res = :DDD_RES, tel_res = :TEL_RES '+
'where cpf = :CPF';
while not cdsOrigem.eof do
begin
cdsDestino.params.parambyname('DDD_RES').AsString := cdsOrigemDDD.AsString;
cdsDestino.params.parambyname('TEL_RES').AsString := cdsOrigemTELEFONE.AsString;
cdsDestino.params.parambyname('CPF').AsString := cdsOrigemCPF.AsString;
cdsDestino.ExecSQL;
cdsOrigem.next;
end;
end;
Gostei + 0
07/04/2010
Ricardo Araujo
segue o codigo este jeito é o mais simples.
qualquer coisa segue meu Email : rbbarreto@hotmail.com
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, DB, DBTables;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
BitBtn1: TBitBtn;
Destino: TQuery;
Origem: TQuery;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Math;
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
Origem.Open;
Origem.First;
while not Origem.Eof do
begin
Destino.close;
Destino.SQL.Clear;
Destino.SQL.Add('SELECT * FROM DESTINO');
Destino.SQL.Add('WHERE CPF = :P_CPF ');
Destino.ParamByName('p_cpf').AsString := Origem.FieldByName('CPF').AsString;
Destino.Open;
if Destino.RecordCount > 0 then
begin
Destino.close;
Destino.SQL.Clear;
Destino.SQL.Add('update DESTINO');
Destino.SQL.Add(' set ddd_res = :ddd_res , ');
Destino.SQL.Add(' tel_res = :tel_res ');
Destino.SQL.Add('where cpf = :p_cpf ');
Destino.ParamByName('ddd_res').AsString := Origem.FieldByName('ddd').AsString;
Destino.ParamByName('tel_res').AsString := Origem.FieldByName('telefones').AsString;
Destino.ParamByName('p_cpf').AsString := Origem.FieldByName('CPF').AsString;
Destino.ExecSQL;
Origem.Next;
end;
Origem.Next;
end;
end;
end.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)