USE master
GO
DECLARE @DbName nvarchar(MAX)
SET @DbName = N'DataBase'
ALTER DATABASE @DbName
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE @DbName SET OFFLINE WITH NO_WAIT
GO
ALTER DATABASE @DbName SET ONLINE
GO
ALTER DATABASE @DbName
SET MULTI_USER
GO
我知道可以使用EXEC,但这有点丑陋...