October 22, 2021

Samir Daoudi's Technical Blog

SharePoint, Office 365, C#, .Net, SQL Server News, Tips, Ebooks, Events, Webinars ….etc

Delete all User databases with TSQL

Again, if you want to delete all user databases with a single TSQL script, you can achieve this easily by using this script

DECLARE @command nvarchar(max)

SET @command = ''

SELECT  @command = @command

+ 'ALTER DATABASE [' + [name] + ']  SET single_user with rollback immediate;'+CHAR(13)+CHAR(10)

+ 'DROP DATABASE [' + [name] +'];'+CHAR(13)+CHAR(10)

FROM  [master].[sys].[databases] 

 where [name] not in ( 'master', 'model', 'msdb', 'tempdb');

SELECT @command

EXECUTE sp_executesql @command

Please ensure to backup all you databases before (how to back up everything)

Use this script at your own risk

Leave a comment

Copyright © All rights reserved. | Newsphere by AF themes.
Show Buttons
Hide Buttons

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.