Example 1

USE [master]
GO
IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'Database01')
DROP DATABASE Database01
GO

CREATE DATABASE [Database01]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'Database01',    FILENAME = N'C:\MSSQL\Data\Database01.mdf' ,      SIZE = 4096KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB)

 LOG ON 
( NAME = N'Database01Log', FILENAME = N'C:\MSSQL\Log\Database01.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB   , FILEGROWTH = 1024KB);
GO  

Example 2

Make sure the following folders are created already.

C:\MSSQL\MyDatabase01\Data

C:\MSSQL\MyDatabase01\Log


GO
USE [master]
GO
DECLARE @kill varchar(8000) = '';  
SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), session_id) + ';'  
--SELECT *
FROM sys.dm_exec_sessions
WHERE database_id  = db_id('MyDatabase01') 

IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'MyDatabase01')
EXEC(@kill);
IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'MyDatabase01')
DROP DATABASE MyDatabase01
GO


CREATE DATABASE [MyDatabase01]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'MyDatabase01',              FILENAME = N'C:\MSSQL\MyDatabase01\Data\MyDatabase01.mdf' ,               SIZE = 24800KB   ,  MAXSIZE = UNLIMITED, FILEGROWTH = 102400KB), 

 FILEGROUP [fgData] 
( NAME = N'fgData_01',        FILENAME = N'C:\MSSQL\MyDatabase01\Data\fgData_01.ndf' ,          SIZE = 24800KB ,  MAXSIZE = UNLIMITED, FILEGROWTH = 1048576KB), 
  FILEGROUP [fgIdx] 
( NAME = N'fgIdx_01',         FILENAME = N'C:\MSSQL\MyDatabase01\Data\fgIdx_01.ndf' ,           SIZE = 24800KB ,  MAXSIZE = UNLIMITED, FILEGROWTH = 1048576KB)

 LOG ON 
( NAME = N'MyDatabase01Log',           FILENAME = N'C:\MSSQL\MyDatabase01\Log\MyDatabase01Log_01.ldf' , SIZE = 24800KB , MAXSIZE = 2048GB , FILEGROWTH = 1048576KB)
GO  

-- put in Single user mode
ALTER DATABASE [MyDatabase01]
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO

IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [MyDatabase01].[dbo].[sp_fulltext_database] @action = 'disable'
end
GO

ALTER DATABASE [MyDatabase01] SET ANSI_NULL_DEFAULT OFF 
GO

ALTER DATABASE [MyDatabase01] SET ANSI_NULLS ON
GO

ALTER DATABASE [MyDatabase01] SET ANSI_PADDING OFF 
GO

ALTER DATABASE [MyDatabase01] SET ANSI_WARNINGS OFF 
GO

ALTER DATABASE [MyDatabase01] SET ARITHABORT OFF 
GO

ALTER DATABASE [MyDatabase01] SET AUTO_CLOSE OFF 
GO

ALTER DATABASE [MyDatabase01] SET AUTO_SHRINK OFF 
GO

ALTER DATABASE [MyDatabase01] SET AUTO_UPDATE_STATISTICS ON 
GO

ALTER DATABASE [MyDatabase01] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO

ALTER DATABASE [MyDatabase01] SET CURSOR_DEFAULT  GLOBAL 
GO

ALTER DATABASE [MyDatabase01] SET CONCAT_NULL_YIELDS_NULL OFF 
GO

ALTER DATABASE [MyDatabase01] SET NUMERIC_ROUNDABORT OFF 
GO

ALTER DATABASE [MyDatabase01] SET QUOTED_IDENTIFIER OFF 
GO

ALTER DATABASE [MyDatabase01] SET RECURSIVE_TRIGGERS OFF 
GO

ALTER DATABASE [MyDatabase01] SET  DISABLE_BROKER 
GO

ALTER DATABASE [MyDatabase01] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO

ALTER DATABASE [MyDatabase01] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO

ALTER DATABASE [MyDatabase01] SET TRUSTWORTHY OFF 
GO

ALTER DATABASE [MyDatabase01] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO

ALTER DATABASE [MyDatabase01] SET PARAMETERIZATION SIMPLE 
GO

ALTER DATABASE [MyDatabase01] SET READ_COMMITTED_SNAPSHOT OFF 
GO

ALTER DATABASE [MyDatabase01] SET HONOR_BROKER_PRIORITY OFF 
GO

ALTER DATABASE [MyDatabase01] SET RECOVERY FULL 
GO

ALTER DATABASE [MyDatabase01] SET PAGE_VERIFY CHECKSUM  
GO

ALTER DATABASE [MyDatabase01] SET DB_CHAINING OFF 
GO

ALTER DATABASE [MyDatabase01] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) 
GO

ALTER DATABASE [MyDatabase01] SET TARGET_RECOVERY_TIME = 0 SECONDS 
GO

ALTER DATABASE [MyDatabase01] SET DELAYED_DURABILITY = DISABLED 
GO

ALTER DATABASE [MyDatabase01] SET  READ_WRITE 
GO

ALTER DATABASE [MyDatabase01] SET  MULTI_USER 
GO

RAISERROR('<<< Finish Script CreateDatabaseMyDatabase01_LocalHost.sql >>>', 0, 1 );
GO

Result:

C:\MSSQL\MyDatabase01\Data\MyDatabase01.mdf

C:\MSSQL\MyDatabase01\Data\fgData_01.ndf

C:\MSSQL\MyDatabase01\Data\fgIdx_01.ndf

C:\MSSQL\MyDatabase01\Log\MyDatabase01Log_01.ldf

Last modified: December 13, 2019

Author

Comments

Write a Reply or Comment