Form inserindo em duas table simultaneamente
Estou desenvolvendo um software de controle de gado de leite para o meu pai e encontrei o seguinte impasse, em determinado form, o usuario poderá trocar a categoria do animal, então ele vai digitar o brinco do animal em um campo que vai pesquisar trazer o nome em um edit, selecionar a nova categoria, e salvar, nisto terá que salvar no banco de dados o registro de edição em uma tabela e o animal com a nova categoria em outra tabela, mas não sei como...
Delphi XE5, Firebird, DBexpress
Delphi XE5, Firebird, DBexpress
Luís Maruccio
Curtidas 0
Respostas
Francisco Andrade
02/11/2016
//Veja meu código e adapte a sua necessidade
try
if not DTModule.ZConexao.InTransaction then
DTModule.ZConexao.StartTransaction;
ZSQLUpdate.Script.Clear;
//Atualizando
ZSQLUpdate.Script.Add(''UPDATE item SET Entregues = '' + VP(vTotalEntregue) + '','' +
'' UltimaEntrega = '' + VP(vEntreagarAgora) + '','' +
'' SaldoEntregar = '' + VP(QrItemquantidade.Value - VTotalEntregue) +
'' WHERE item.idvenda = '' + QuotedStr(CbVenda.Text) +
'' AND item.iditem = '' + QuotedStr(QrEntreguesiditem.Text) +'';'');
//Inserindo
ZSQLUpdate.Script.Add(''INSERT INTO historicoentregas ''+
''(NomeEntregador, DataVenda, DataEntrega, IdProduto, '' +
'' NomeProduto, QuantidadeEntregue, ''+
'' Idcliente, NomeCliente, IdVenda ) values ('' +
QuotedStr(vRecebedor) + '','' +
QuotedStr(DataEUA(QrVendadata.Value)) + '','' +
QuotedStr(DataEUA(date)) + '','' +
IntToStr(QrEntreguesiditem.Value) + '','' +
QuotedStr(QrEntreguesNomeProduto.Text) + '','' +
VP(vEntreagarAgora) + '','' +
IntToStr(QrVendaidcliente.Value) + '','' +
QuotedStr(QrVendaNomeCliente.Value) + '','' +
IntToStr(QrVendaidvenda.Value) + '');'');
ZSQLUpdate.Execute;
DTModule.ZConexao.Commit;
Except on E:Exception do
begin
// Cancelando ação nas duas tabelas caso haja erro em uma delas
DTModule.ZConexao.Rollback;
ShowMessage(E.Message);
ShowMessage(ZSQLUpdate.Script.Text);
exit;
end;
end;
try
if not DTModule.ZConexao.InTransaction then
DTModule.ZConexao.StartTransaction;
ZSQLUpdate.Script.Clear;
//Atualizando
ZSQLUpdate.Script.Add(''UPDATE item SET Entregues = '' + VP(vTotalEntregue) + '','' +
'' UltimaEntrega = '' + VP(vEntreagarAgora) + '','' +
'' SaldoEntregar = '' + VP(QrItemquantidade.Value - VTotalEntregue) +
'' WHERE item.idvenda = '' + QuotedStr(CbVenda.Text) +
'' AND item.iditem = '' + QuotedStr(QrEntreguesiditem.Text) +'';'');
//Inserindo
ZSQLUpdate.Script.Add(''INSERT INTO historicoentregas ''+
''(NomeEntregador, DataVenda, DataEntrega, IdProduto, '' +
'' NomeProduto, QuantidadeEntregue, ''+
'' Idcliente, NomeCliente, IdVenda ) values ('' +
QuotedStr(vRecebedor) + '','' +
QuotedStr(DataEUA(QrVendadata.Value)) + '','' +
QuotedStr(DataEUA(date)) + '','' +
IntToStr(QrEntreguesiditem.Value) + '','' +
QuotedStr(QrEntreguesNomeProduto.Text) + '','' +
VP(vEntreagarAgora) + '','' +
IntToStr(QrVendaidcliente.Value) + '','' +
QuotedStr(QrVendaNomeCliente.Value) + '','' +
IntToStr(QrVendaidvenda.Value) + '');'');
ZSQLUpdate.Execute;
DTModule.ZConexao.Commit;
Except on E:Exception do
begin
// Cancelando ação nas duas tabelas caso haja erro em uma delas
DTModule.ZConexao.Rollback;
ShowMessage(E.Message);
ShowMessage(ZSQLUpdate.Script.Text);
exit;
end;
end;
GOSTEI 0