Operation cannot be performed on an active dataset lazarus / 16-Databases ---------------------------------------------------------------------- martinrame · 2018-06-21 22:29 Hi, I'm trying to do a Select, loop through its records, then call a procedure to update each record, but I'm getting "Operation cannot be performed on an active dataset".  I wonder why this error, since my "UpdateRecords" procedure creates a new TSqlQuery instance. Here's my code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure ListRecords;var                                                                                                                                           lSql: TSqlQuery;           id: Integer;                                                                                                                 begin                                                                                                                                         lSql := TSqlQuery.Create(nil);                                                                                                              try                                                                                                                                           lSql.DataBase := DataModule1.PQConnection1;                                                                                                lSql.SQL.Text := 'select * from table';                                                              lSql.Open;                                                                                                                                  while not lSql.Eof do                                                                                                                       begin                                   id := lSql.FieldByName('id').AsInteger;                                                                                                                                                                                                              UpdateRecord(id);                                                                                                      lSql.Next;                                                                                                                                end;                                                                                                                                      finally                                                                                                                                       lSql.Free;                                                                                                                                end;                                                                                                                                      end;                Procedure UpdateRecords(Id: Integer);var                                                                                                                                           lSql: TSqlQuery;                                                                                                                          begin                                                                                                                                         lSql := TSqlQuery.Create(nil);                                                                                                              try                                                                                                                                           lSql.DataBase := DataModule1.PQConnection1;                                                                                              lSql.Transaction := DataModule1.SqlTransaction1;                                                                                                lSql.SQL.Text := 'update table set read=true where id=:id';    lSql.ParamByName('id').AsInteger := id;                                                              lSql.ExecSql;    DataModule1.SqlTransaction1.Commit;                                                                                                                                finally                                                                                                                                       lSql.Free;                                                                                                                                end;                                                                                                                                      end; ---------------------------------------------------------------------- valdir.marcos · 2018-06-22 03:16 Why are you slowly updating all that on the client side instead of using the faster server side? Code: SQL  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i UPDATE TABLE SET READ=TRUE WHERE id IN (SELECT id FROM TABLE WHERE condition = :condition); ---------------------------------------------------------------------- martinrame · 2018-06-22 12:01 This is an example, not real code. Inside my ListRecords function I do many more operations (that cannot be done in the database) than just updating records from the same table. ---------------------------------------------------------------------- martinrame · 2018-06-22 12:11 Sorry, the error does not comes from this code, but from a nested query I'm doing against an SQLServer Database. I'll try to isolate it, then come bak. ---------------------------------------------------------------------- martinrame · 2018-06-22 12:18 The solution was NOT commiting inside the nested procedure (the one tha does .ExecSQL), but commiting when the main loop ends. ---------------------------------------------------------------------- rvk · 2018-06-22 12:19 My guess is that DataModule1.SqlTransaction1 is also connected to the query in ListRecords (via the database connection in DataModule1). Committing that transaction will also close the query in ListRecords. You need to re-think the flow or set the correct parameter for the transaction that it will not close the queries automatically. Rethinking the flow is the better choice. Maybe use a separate transaction in UpdateRecords(). Edit: Ah, you already found it