

PRINT 'Start Batch File Create'
Go

Print 'File: C:\Builds\EnterpriseReporting\Version6.4\source\database\DB1_Staging\Create Scripts\Database\DB1_Staging.dbo.CreateDB1_StagingDB.sql'
Go

SET NOCOUNT ON

DECLARE @MDFLocation nvarchar(1000),
	@LDFLocation nvarchar(1000)

CREATE TABLE #Location
(Value VARCHAR(30),
 Data VARCHAR(1000))

INSERT #Location
EXEC master..xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\MSSQLServer',N'DefaultData'

SELECT @MDFLocation = Data + 'DB1_Staging_Data.MDF'
FROM  #Location

IF @MDFLocation IS NULL
BEGIN
	SELECT	@MDFLocation = replace(filename, 'model.mdf','') + 'DB1_Staging_Data.MDF'
	FROM master.dbo.sysdatabases
	WHERE [name] = 'model'
END

DELETE #Location

INSERT #Location
EXEC master..xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\MSSQLServer',N'DefaultLog'

SELECT @LDFLocation = Data + 'DB1_Staging_Log.LDF'
FROM  #Location

IF @LDFLocation IS NULL
BEGIN
	SELECT	@LDFLocation = replace(filename, 'model.mdf','') + 'DB1_Staging_Log.LDF'
	FROM master.dbo.sysdatabases
	WHERE [name] = 'model'
END

Drop table #Location

IF NOT EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE [name] = 'DB1_Staging')
BEGIN
	PRINT 'Creating Database DB1_Staging'
	EXEC ('CREATE DATABASE [DB1_Staging]  ON (NAME = N''DB1_Staging_Data'', FILENAME = N'''+ @MDFLocation + ''' , SIZE = 10, FILEGROWTH = 10%) LOG ON (NAME = N''DB1_Staging_Log'', FILENAME = N''' + @LDFLocation + ''' , SIZE = 2, FILEGROWTH = 10%) COLLATE SQL_Latin1_General_CP1_CI_AS')
END
GO

exec sp_dboption N'DB1_Staging', N'autoclose', N'false'
GO

exec sp_dboption N'DB1_Staging', N'bulkcopy', N'false'
GO

exec sp_dboption N'DB1_Staging', N'trunc. log', N'false'
GO

exec sp_dboption N'DB1_Staging', N'torn page detection', N'true'
GO

exec sp_dboption N'DB1_Staging', N'read only', N'false'
GO

exec sp_dboption N'DB1_Staging', N'dbo use', N'false'
GO

exec sp_dboption N'DB1_Staging', N'single', N'false'
GO

exec sp_dboption N'DB1_Staging', N'autoshrink', N'false'
GO

exec sp_dboption N'DB1_Staging', N'ANSI null default', N'false'
GO

exec sp_dboption N'DB1_Staging', N'recursive triggers', N'false'
GO

exec sp_dboption N'DB1_Staging', N'ANSI nulls', N'false'
GO

exec sp_dboption N'DB1_Staging', N'concat null yields null', N'false'
GO

exec sp_dboption N'DB1_Staging', N'cursor close on commit', N'false'
GO

exec sp_dboption N'DB1_Staging', N'default to local cursor', N'false'
GO

exec sp_dboption N'DB1_Staging', N'quoted identifier', N'false'
GO

exec sp_dboption N'DB1_Staging', N'ANSI warnings', N'false'
GO

exec sp_dboption N'DB1_Staging', N'auto create statistics', N'true'
GO

exec sp_dboption N'DB1_Staging', N'auto update statistics', N'true'
GO

if( ( (@@microsoftversion / power(2, 24) = 8) and (@@microsoftversion & 0xffff >= 724) ) or ( (@@microsoftversion / power(2, 24) = 7) and (@@microsoftversion & 0xffff >= 1082) ) )
	exec sp_dboption N'DB1_Staging', N'db chaining', N'false'
GO

Print 'File: C:\Builds\EnterpriseReporting\Version6.4\source\database\DB1\Create Scripts\Database\DB1.dbo.CreateDB1DB.sql'
Go

SET NOCOUNT ON

DECLARE @MDFLocation nvarchar(1000),
	@LDFLocation nvarchar(1000)

CREATE TABLE #Location
(Value VARCHAR(30),
 Data VARCHAR(1000))

INSERT #Location
EXEC master..xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\MSSQLServer',N'DefaultData'

SELECT @MDFLocation = Data + 'DB1_Data.MDF'
FROM  #Location

IF @MDFLocation IS NULL
BEGIN
	SELECT	@MDFLocation = replace(filename, 'model.mdf','') + 'DB1_Data.MDF'
	FROM master.dbo.sysdatabases
	WHERE [name] = 'model'
END

DELETE #Location

INSERT #Location
EXEC master..xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\MSSQLServer',N'DefaultLog'

SELECT @LDFLocation = Data + 'DB1_Log.LDF'
FROM  #Location

IF @LDFLocation IS NULL
BEGIN
	SELECT	@LDFLocation = replace(filename, 'model.mdf','') + 'DB1_Log.LDF'
	FROM master.dbo.sysdatabases
	WHERE [name] = 'model'
END

Drop table #Location

IF NOT EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE [name] = 'DB1')
BEGIN
	PRINT 'Creating Database DB1'
	EXEC ('CREATE DATABASE [DB1]  ON (NAME = N''DB1_Data'', FILENAME = N'''+ @MDFLocation + ''' , SIZE = 10, FILEGROWTH = 10%) LOG ON (NAME = N''DB1_Log'', FILENAME = N''' + @LDFLocation + ''' , SIZE = 2, FILEGROWTH = 10%) COLLATE SQL_Latin1_General_CP1_CI_AS')
END
GO

exec sp_dboption N'DB1', N'autoclose', N'false'
GO

exec sp_dboption N'DB1', N'bulkcopy', N'false'
GO

exec sp_dboption N'DB1', N'trunc. log', N'false'
GO

exec sp_dboption N'DB1', N'torn page detection', N'true'
GO

exec sp_dboption N'DB1', N'read only', N'false'
GO

exec sp_dboption N'DB1', N'dbo use', N'false'
GO

exec sp_dboption N'DB1', N'single', N'false'
GO

exec sp_dboption N'DB1', N'autoshrink', N'false'
GO

exec sp_dboption N'DB1', N'ANSI null default', N'false'
GO

exec sp_dboption N'DB1', N'recursive triggers', N'false'
GO

exec sp_dboption N'DB1', N'ANSI nulls', N'false'
GO

exec sp_dboption N'DB1', N'concat null yields null', N'false'
GO

exec sp_dboption N'DB1', N'cursor close on commit', N'false'
GO

exec sp_dboption N'DB1', N'default to local cursor', N'false'
GO

exec sp_dboption N'DB1', N'quoted identifier', N'false'
GO

exec sp_dboption N'DB1', N'ANSI warnings', N'false'
GO

exec sp_dboption N'DB1', N'auto create statistics', N'true'
GO

exec sp_dboption N'DB1', N'auto update statistics', N'true'
GO

if( ( (@@microsoftversion / power(2, 24) = 8) and (@@microsoftversion & 0xffff >= 724) ) or ( (@@microsoftversion / power(2, 24) = 7) and (@@microsoftversion & 0xffff >= 1082) ) )
	exec sp_dboption N'DB1', N'db chaining', N'false'
GO
