Fórum [Sql] - Insert ou Update #282005

17/05/2005

0

Boa Noite,
Gente eu estou com um problema que é o seguinte:

Eu ao clicar no botao Salvar ele tem que verificar se o que tem no Edit1 já existe na tabela, se já existir ele da update no registro se nao ele dá insert.

Só que mesmo sabendo que já existe o registro ´N´ ele manda Inserir, onde deveria fazer update direto.


o codigo é o seguinte:

procedure TForm1.Button1Click(Sender: TObject);
begin
   qryclientes.SQL.Clear;
   qryclientes.SQL.Add(´Select * from Nvocc_Navioviagem´);
   qryclientes.SQL.Add(´Where VIAGEM <> ´ + quotedstr(edit1.Text));
   qryclientes.ExecSQL;
   if qryclientes.Eof then
      begin
         qryclientes.sql.clear;
         qryclientes.sql.add(´INSERT INTO Nvocc_Navioviagem (NAVIO, VIAGEM,´);
         qryclientes.sql.add(´ENTRADA, OPERACAO, SAIDA,TRANSPORTADOR, ARMADOR,´);
         qryclientes.sql.add(´ATRACACAO, MOEDA, VALOR, VENCIMENTO, FINANCIADO) ´);
         qryclientes.sql.add(´ VALUES (´ +
            quotedstr(ComboBox1.Text) + ´,´ +
            quotedstr(edit1.Text) + ´,´ +
            quotedstr(edit2.Text) + ´,´ +
            quotedstr(edit3.text) + ´,´ +
            quotedstr(edit4.text) + ´,´ +
            quotedstr(edit5.text) + ´,´ +
            quotedstr(edit6.text) + ´,´ +
            quotedstr(ComboBox2.Text) + ´,´ +
            quotedstr(ComboBox3.Text) + ´,´ +
            quotedstr(edit7.Text) + ´,´ +
            quotedstr(edit8.Text) + ´,´ +
            quotedstr(edit9.Text) + ´)´);
         qryclientes.execsql;
      end
   else
   try
      if Application.MessageBox(´Estes dados já existe no Banco de Dados, Deseja Salvar mesmo assim?´,
         ´Confirmação´, MB_ICONQUESTION + MB_YESNO + MB_DEFBUTTON2) = idyes then

      qryclientes.sql.clear;
      qryclientes.sql.add(´UPDATE Nvocc_Navioviagem´);
      qryclientes.sql.add(´set NAVIO =´ + quotedstr(ComboBox1.Text) + ´,´);
      qryclientes.sql.add(´VIAGEM = ´ + quotedstr(edit1.Text) + ´,´);
      qryclientes.sql.add(´ENTRADA = ´ + quotedstr(edit2.Text) + ´,´);
      qryclientes.sql.add(´OPERACAO = ´ + quotedstr(edit3.Text) + ´,´);
      qryclientes.sql.add(´SAIDA = ´ + quotedstr(edit4.Text) + ´,´);
      qryclientes.sql.add(´TRANSPORTADOR = ´ + quotedstr(edit5.Text) + ´,´);
      qryclientes.sql.add(´ARMADOR = ´ + quotedstr(edit6.Text) + ´,´);
      qryclientes.sql.add(´ATRACACAO = ´ + quotedstr(ComboBox2.Text) + ´,´);
      qryclientes.sql.add(´MOEDA = ´ + quotedstr(ComboBox3.Text) + ´,´);
      qryclientes.sql.add(´VALOR = ´ + quotedstr(edit7.Text) + ´,´);
      qryclientes.sql.add(´VENCIMENTO = ´ + quotedstr(edit8.Text) + ´,´);
      qryclientes.sql.add(´FINANCIADO = ´ + quotedstr(edit9.Text));
      qryclientes.sql.Add(´where VIAGEM = ´ + quotedstr(edit1.text));
      qryclientes.execsql;
   except
      messageBeep(10);
   end;
end;



Muito obrigado!!!
Fernando Cunha
Obs.: os outros Topicos meu eu já consegui Resolver obrigado.


Fernando_cunha

Fernando_cunha

Responder

Posts

17/05/2005

Melo

Cara, dei uma olhada rapida no seu codigo e fiz algumas modificações, mas não garanto 100¬ de funcionamento :roll: , qq coisa e so postar no forum que irei te ajudar :D

procedure TForm1.Button1Click(Sender: TObject); 
begin 
   qryclientes.SQL.Clear; 
   qryclientes.SQL.Add(´Select * from Nvocc_Navioviagem´); 
   qryclientes.SQL.Add(´Where VIAGEM <> ´ + quotedstr(edit1.Text)); 
   qryclientes.Open; -> Modifiquei Aki
   if qryclientes.isEmpty then  -> Modifiquei Aki
      begin 
         qryclientes.close;  -> Modifiquei Aki
         qryclientes.sql.clear; 
         qryclientes.sql.add(´INSERT INTO Nvocc_Navioviagem (NAVIO, VIAGEM,´); 
         qryclientes.sql.add(´ENTRADA, OPERACAO, SAIDA,TRANSPORTADOR, ARMADOR,´); 
         qryclientes.sql.add(´ATRACACAO, MOEDA, VALOR, VENCIMENTO, FINANCIADO) ´); 
         qryclientes.sql.add(´ VALUES (´ + 
            quotedstr(ComboBox1.Text) + ´,´ + 
            quotedstr(edit1.Text) + ´,´ + 
            quotedstr(edit2.Text) + ´,´ + 
            quotedstr(edit3.text) + ´,´ + 
            quotedstr(edit4.text) + ´,´ + 
            quotedstr(edit5.text) + ´,´ + 
            quotedstr(edit6.text) + ´,´ + 
            quotedstr(ComboBox2.Text) + ´,´ + 
            quotedstr(ComboBox3.Text) + ´,´ + 
            quotedstr(edit7.Text) + ´,´ + 
            quotedstr(edit8.Text) + ´,´ + 
            quotedstr(edit9.Text) + ´)´); 
         qryclientes.execsql; 
      end 
   else 
   try 
      if Application.MessageBox(´Estes dados já existe no Banco de Dados, Deseja Salvar mesmo assim?´, 
         ´Confirmação´, MB_ICONQUESTION + MB_YESNO + MB_DEFBUTTON2) = idyes then 
      begin  
        qryclientes.close;  -> Modifiquei Aki
        qryclientes.sql.clear; 
        qryclientes.sql.add(´UPDATE Nvocc_Navioviagem´); 
        qryclientes.sql.add(´set NAVIO =´ + quotedstr(ComboBox1.Text) + ´,´); 
        qryclientes.sql.add(´VIAGEM = ´ + quotedstr(edit1.Text) + ´,´); 
        qryclientes.sql.add(´ENTRADA = ´ + quotedstr(edit2.Text) + ´,´); 
        qryclientes.sql.add(´OPERACAO = ´ + quotedstr(edit3.Text) + ´,´); 
        qryclientes.sql.add(´SAIDA = ´ + quotedstr(edit4.Text) + ´,´); 
        qryclientes.sql.add(´TRANSPORTADOR = ´ + quotedstr(edit5.Text) + ´,´); 
        qryclientes.sql.add(´ARMADOR = ´ + quotedstr(edit6.Text) + ´,´); 
        qryclientes.sql.add(´ATRACACAO = ´ + quotedstr(ComboBox2.Text) + ´,´); 
        qryclientes.sql.add(´MOEDA = ´ + quotedstr(ComboBox3.Text) + ´,´); 
        qryclientes.sql.add(´VALOR = ´ + quotedstr(edit7.Text) + ´,´); 
        qryclientes.sql.add(´VENCIMENTO = ´ + quotedstr(edit8.Text) + ´,´); 
        qryclientes.sql.add(´FINANCIADO = ´ + quotedstr(edit9.Text)); 
        qryclientes.sql.Add(´where VIAGEM = ´ + quotedstr(edit1.text)); 
        qryclientes.execsql; 
      end;  
   except 
      messageBeep(10); 
   end; 
end;



Responder

Gostei + 0

18/05/2005

Fernando_cunha

Bom dia,
Obrigado pela ajuda, mas mesmo assim continua dando errado, quando eu vou salvar um registro q ja existe ele da UPdate normal... so que quando eu vou salvar um novo ele ele pula direto pra rotina de update... ele nem passa pelo insert.


Responder

Gostei + 0

18/05/2005

Fernando_cunha

Bom eu mexendo aqui eu alterei

DE:
if qryclientes.isEmpty then

PARA:
if not qryclientes.isEmpty then

só q ele ao clicar ele insere, depois ele da erro de keyviolation, onde teria q dar update.....


Responder

Gostei + 0

18/05/2005

Rjun

Altere o primeiro select para

Select * from Nvocc_Navioviagem Where VIAGEM = ´ + quotedstr(edit1.Text)); 


Tire esse not que você colocou no post anterior, deixando o if da seguinte maneira

if qryClintes.isEmpty



Responder

Gostei + 0

18/05/2005

Fernando_cunha

Obrigado, Funcionou beleza.


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar