sql tsql__template_create_database.sql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql tsql__template_create_database.sql相关的知识,希望对你有一定的参考价值。

-- =============================================
-- Create database
-- =============================================
USE master
GO

DECLARE 
	 @dbName nvarchar(128)
	,@deviceDirectory nvarchar(max)
	,@recovery varchar(50)
SELECT 
	 @dbName = 'Staging'
	,@recovery = 'SIMPLE'
	,@deviceDirectory = NULL

-- DEFAULT: Use directory of master database
IF ( RTRIM(LTRIM(ISNULL(@deviceDirectory, ''))) = '' )
BEGIN
	SELECT @deviceDirectory = SUBSTRING(filename, 1, CHARINDEX(N'master.mdf', LOWER(filename)) - 1)
	FROM master.dbo.sysaltfiles WHERE dbid = 1 AND fileid = 1
END

IF EXISTS ( SELECT * FROM sysdatabases WHERE name = @dbName)
BEGIN
	EXEC('DROP DATABASE ' + @dbName)
END

EXECUTE (N'CREATE DATABASE ' + @dbName + ' 
  ON PRIMARY (NAME = N''' + @dbName + ''', FILENAME = N''' + @deviceDirectory + N'' + @dbName + '.mdf'')
  LOG ON (NAME = N''' + @dbName + '_log'',  FILENAME = N''' + @deviceDirectory + N'' + @dbName + '.ldf'')')


EXEC (
	'ALTER DATABASE ' + @dbName + '
	 SET RECOVERY ' + @recovery
)
GO

以上是关于sql tsql__template_create_database.sql的主要内容,如果未能解决你的问题,请参考以下文章

sql tsql__get_schema.sql

sql tsql__template_create_database.sql

sql tsql__SQLCMD_mode_check.sql

sql tsql__get_lock_history.sql

sql tsql__kill_idle_session.sql

sql tsql__kill_db_connections.sql