Skip to content

Instantly share code, notes, and snippets.

@ChauThan
Last active March 22, 2024 14:51
Show Gist options
  • Save ChauThan/26d1b5c0e6387acb4bfc7e332eb0b7de to your computer and use it in GitHub Desktop.
Save ChauThan/26d1b5c0e6387acb4bfc7e332eb0b7de to your computer and use it in GitHub Desktop.
[RecreateDatabase] #sqlserver #sql
EXEC sp_RecreateDatabase @DatabaseName = N'YourDatabase'; -- Thay 'YourDatabase' bằng tên cơ sở dữ liệu của bạn
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