Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Firebird e Interbase (https://www.clubdelphi.com/foros/forumdisplay.php?f=19)
-   -   Insert con una variable entera (https://www.clubdelphi.com/foros/showthread.php?t=68141)

MAXIUM 27-05-2010 05:44:24

Insert con una variable entera
 
Estoy usando el componente IBSQL y quiero hacer un insert usando una variable de tipo entero, pero me arroja error.

Código Delphi [-]
Var
    Total: Integer;
Begin

Total:= 435;

with Consulta do
  begin
    SQL.Clear;
    SQL.Add( 'INSERT INTO CLIENTES' );
    SQL.Add( '( NOMBRE, NIF, IMPORTEPTE )' );
    SQL.Add( 'VALUES' );
    SQL.Add( '( ''ANTONIO GARCIA LOPEZ'', ''46876283D'', Total )' );

    Transaction.StartTransaction;

    try
      ExecQuery;
      Transaction.Commit;
    except
      on E: Exception do
      begin
        Application.MessageBox( PChar( E.Message ), 'Error de SQL', MB_ICONSTOP );
        Transaccion.Rollback;
      end;
    end;
  end;
End;

roman 27-05-2010 06:36:03

Tú simplemente le estás pasando la palabra Total al comando SQL y no el valor de la variable Total. Tendrías que concatenar:

Código Delphi [-]
'( ''ANTONIO GARCIA LOPEZ'', ''46876283D'', ' + IntToStr(Total) + ')'

Aunque siempre será mejor pasar el valor como parámetro:

Código Delphi [-]
SQL.Add( '( ''ANTONIO GARCIA LOPEZ'', ''46876283D'', :total )' );
ParamByName('total').AsInteger := Total;

// Saludos

MAXIUM 28-05-2010 01:07:10

Excelente, muchas gracias;


La franja horaria es GMT +2. Ahora son las 06:42:21.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi