Skip to content

Commit 62caaf9

Browse files
committed
fix: MS SQL throws "Cannot drop database xyz, because it is currently in use" when user is about to drop the current database
Sets the next or previous sibling database, whichever is available Refs #1436
1 parent 09c6afa commit 62caaf9

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

source/main.pas

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3861,7 +3861,7 @@ procedure TMainForm.actInsertFilesExecute(Sender: TObject);
38613861
procedure TMainForm.actDropObjectsExecute(Sender: TObject);
38623862
var
38633863
msg, db: String;
3864-
Node: PVirtualNode;
3864+
Node, SiblingDB: PVirtualNode;
38653865
Obj: PDBObject;
38663866
DBObject: TDBObject;
38673867
ObjectList: TDBObjectList;
@@ -3882,7 +3882,15 @@ procedure TMainForm.actDropObjectsExecute(Sender: TObject);
38823882
try
38833883
db := DBObject.Database;
38843884
Node := FindDBNode(DBtree, Conn, db);
3885-
SetActiveDatabase('', Conn);
3885+
// Set focus on previous or next database, to prevent "Cannot drop database xyz, because it is currently in use"
3886+
// MS SQL on top cannot "un-use" the current database
3887+
SiblingDB := DBtree.GetNextSibling(Node);
3888+
if not Assigned(SiblingDB) then
3889+
SiblingDB := DBtree.GetPreviousSibling(Node);
3890+
if Assigned(SiblingDB) then
3891+
SetActiveDatabase(DBtree.Text[SiblingDB, 0], Conn)
3892+
else
3893+
SetActiveDatabase('', Conn); // Fallback if there is no sibling. Works on MySQL only.
38863894
Conn.Query(qDatabaseDrop, [Conn.QuoteIdent(db)]);
38873895
DBtree.DeleteNode(Node);
38883896
Conn.ClearDbObjects(db);

0 commit comments

Comments
 (0)