DON'T use Locate(). If you use locate() then Delphi searches row on the client side just scanning row set from your query it takes a LOT of time.
If you have access to MSSQL to create stored procedures then create following procedure and just run it for each line from your TEXT file without any conditions (Use TAdoStoredProc.ExecProc in Delphi). So in this case your don't need first select and Locate procedure. It updates record if Filed1 and Field2 are found and insert if don't.
CREATE PROCEDURE dbo.update_table1
@Field1 int, --key1
@Field2 int, --key2
@Field3 int, -- data fileds
@Field4 int
AS
SET NOCOUNT ON
update table1 set Field3=@Field3,Field4=@Field4
where Field1=@Field1 and Field2=@Field2;
IF(@@Rowcount=0)
BEGIN
insert into table1(Field1,Field2,Field3,Field4)
values (@Field1,@Field2,@Field3,@Field4);
END
GO
Here is Delphi code to invoke this stored procedure with ADO:
......
var
ADOStoredP: TADOStoredProc;
......
begin
........
ADOStoredP:=TADOStoredProc.Create(nil);
try
ADOStoredP.Connection:=DataMod.SQL_ADOConnection; //Your ADO Connection instance here
ADOStoredP.ProcedureName:='Update_table1';
ADOStoredP.Parameters.CreateParameter('@Field1', ftInteger, pdInput, 0, 0);
ADOStoredP.Parameters.CreateParameter('@Field2', ftInteger, pdInput, 0, 0);
ADOStoredP.Parameters.CreateParameter('@Field3', ftInteger, pdInput, 0, 0);
ADOStoredP.Parameters.CreateParameter('@Field4', ftInteger, pdInput, 0, 0);
While () -- Your text file loop here
begin
ADOStoredP.Parameters.ParamByName('@Field1').Value:=Field1 value from text file here;
ADOStoredP.Parameters.ParamByName('@Field2').Value:=Field2 value from text file here;
ADOStoredP.Parameters.ParamByName('@Field3').Value:=Field3 value from text file here;
ADOStoredP.Parameters.ParamByName('@Field4').Value:=Field4 value from text file here;
ADOStoredP.ExecProc;
end
finally
if Assigned(ADOStoredP) then
begin
ADOStoredP.Free;
end;
end;
........
end;