uses db, sqldb, ..... ;
Function QueryDB(Const aDB :TCustomConnection; aCommand :String; Const aTransaction :TSQLTransaction) :TSQLQuery;
begin
Result := TSQLQuery.Create(nil);//<- set it to either self or the component that will free it when finished its job
Result.DataBase := aDB;
if Assigned(aTransaction) then Result.Transaction := aTransaction;
Result.SQL.Text := aCommand;
Result.Open;
end;