How to drop multiple databases in SQL Server

后端 未结 3 1661
梦如初夏
梦如初夏 2020-12-24 02:19

Just to clarify, ths isn\'t really a question, more some help for people like me who were looking for an answer.
A lot of applications create temp tables and the like, b

3条回答
  •  被撕碎了的回忆
    2020-12-24 02:52

    There is no need to use a cursor, and no need to copy and paste SQL statements. Just run these two lines:

    DECLARE @Sql as NVARCHAR(MAX) = (SELECT 'DROP DATABASE ['+ name + ']; ' FROM sys.databases WHERE name LIKE 'DBName%' FOR XML PATH(''))

    EXEC sys.sp_executesql @Sql

    Of course, any DB matching the criteria will be dropped immediately, so be sure that you know what you are doing.

提交回复
热议问题