Last active
March 22, 2024 14:51
-
-
Save ChauThan/26d1b5c0e6387acb4bfc7e332eb0b7de to your computer and use it in GitHub Desktop.
[RecreateDatabase] #sqlserver #sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EXEC sp_RecreateDatabase @DatabaseName = N'YourDatabase'; -- Thay 'YourDatabase' bằng tên cơ sở dữ liệu của bạn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE PROCEDURE sp_RecreateDatabase | |
@DatabaseName nvarchar(50) | |
AS | |
BEGIN | |
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = @DatabaseName) | |
BEGIN | |
EXEC('ALTER DATABASE [' + @DatabaseName + '] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;'); | |
EXEC('DROP DATABASE [' + @DatabaseName + '];'); | |
END | |
EXEC('CREATE DATABASE [' + @DatabaseName + '];'); | |
EXEC('ALTER DATABASE [' + @DatabaseName + '] SET RECOVERY SIMPLE;'); | |
END | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment