Skip to content

Commit 26ea358

Browse files
committedMar 10, 2020
Avoid recreating the LocalDB instance by detaching orphaned databases
1 parent 9bad189 commit 26ea358

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed
 

‎tools/CleanMSSQLLocalDB.cmd

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
@echo off
2-
sqlcmd -S "(localdb)\mssqllocaldb" -i "DropAllDatabases.sql" -o "DropAll.sql"
2+
sqlcmd -S "(localdb)\mssqllocaldb" -i "%~dp0DropAllDatabases.sql" -o "DropAll.sql"
33
sqlcmd -S "(localdb)\mssqllocaldb" -i "DropAll.sql"
44
del "DropAll.sql"
5-
sqllocaldb stop mssqllocaldb
6-
sqllocaldb delete mssqllocaldb
75

86
%~dp0ShrinkLocalDBModel.cmd

‎tools/DropAllDatabases.sql

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ WHILE @@FETCH_STATUS = 0
1515
BEGIN
1616
PRINT 'DROP DATABASE [' + REPLACE( @name, ']', ']]' ) + ']'
1717
PRINT 'GO'
18+
19+
PRINT 'IF(db_id(''' + REPLACE( @name, '''', '''''') + ''') IS NOT NULL)'
20+
PRINT 'EXEC sp_detach_db ''' + REPLACE( @name, '''', '''''') + ''''
21+
PRINT 'GO'
1822

1923
FETCH NEXT FROM db
2024
INTO @name

0 commit comments

Comments
 (0)