Problem: I need to change MSSQL Server's login password by fpc code. Unforunately, MSSQL Server tells me that sp_password can NOT be used in a transaction.
I looked on the wiki document
https://wiki.lazarus.freepascal.org/SQLdb_Programming_Reference, it tells that every command must be in a transaction.
So, how to do?
Yes, we can define a fake transaction class to cheat the db driver
Below is the code:
type
{ TFakeTransaction }
TFakeTransaction = class(TSQLTransaction)
public
procedure Commit; override;
procedure CommitRetaining; override;
procedure Rollback; override;
procedure RollbackRetaining; override;
procedure StartTransaction; override;
procedure EndTransaction; override;
end;
implementation
{ TFakeTransaction }
procedure TFakeTransaction.Commit;
begin
EndTransaction;
end;
procedure TFakeTransaction.CommitRetaining;
begin
// do nothing
end;
procedure TFakeTransaction.Rollback;
begin
EndTransaction;
end;
procedure TFakeTransaction.RollbackRetaining;
begin
// do nothing
end;
procedure TFakeTransaction.StartTransaction;
begin
OpenTrans; // Just set the active flag
end;
procedure TFakeTransaction.EndTransaction;
begin
CloseTrans; // Just clear the active flag
end;
The TFakeTransaction just set/clear the FActive flag which defined in TDBTransaction class privatly on need, nothing else to do.
So, you can do something without a transaction now.
Maybe helpful and enjoy FPC