0

I am trying to create a database through powershell. The problem I'm encountering is that I want this database to be created with some flags enabled (for example "Allow Snapshot Isolation"). I do not know how to do that and I'm having trouble finding out.

What I have done so far:

$SqlSecurePassword = $SqlServerPassword | ConvertTo-SecureString -asPlainText -Force
$Connection = new-Object Microsoft.SqlServer.Management.Common.ServerConnection("(local)", $SqlServerUser, $SqlServerPassword)
$Server = new-Object Microsoft.SqlServer.Management.Smo.Server($Connection)
$Db = new-Object Microsoft.SqlServer.Management.Smo.Database($Server, $DatabaseName)
$Db.Create()
3
  • How about executing the needed ALTER DATABASE statements after creating the DB? Commented Jun 23, 2015 at 7:57
  • That is also an option, but I was hoping the flags were part of the object, Something like $Db.SomeFlag = $True Commented Jun 23, 2015 at 8:41
  • hmmm, turns out they actually can be set like that Commented Jun 23, 2015 at 10:44

3 Answers 3

1

I would wirte a SQL-Script to create the database, just like this:

CREATE DATABASE [test]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'test', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\test.mdf' , SIZE = 5120KB , FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'test_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\test_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10%)
GO
ALTER DATABASE [test] SET COMPATIBILITY_LEVEL = 120
GO
ALTER DATABASE [test] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [test] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [test] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [test] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [test] SET ARITHABORT OFF 
GO
ALTER DATABASE [test] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [test] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [test] SET AUTO_CREATE_STATISTICS ON(INCREMENTAL = OFF)
GO
ALTER DATABASE [test] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [test] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [test] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [test] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [test] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [test] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [test] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [test] SET  DISABLE_BROKER 
GO
ALTER DATABASE [test] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [test] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [test] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [test] SET ALLOW_SNAPSHOT_ISOLATION ON
GO
ALTER DATABASE [test] SET READ_COMMITTED_SNAPSHOT ON 
GO
ALTER DATABASE [test] SET  READ_WRITE 
GO
ALTER DATABASE [test] SET RECOVERY SIMPLE 
GO
ALTER DATABASE [test] SET  MULTI_USER 
GO
ALTER DATABASE [test] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [test] SET TARGET_RECOVERY_TIME = 0 SECONDS 
GO
ALTER DATABASE [test] SET DELAYED_DURABILITY = DISABLED 
GO
USE [test]
GO
IF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name = N'PRIMARY') ALTER DATABASE [test] MODIFY FILEGROUP [PRIMARY] DEFAULT
GO

and then execute this file with powershell... Is that an option for you?

Sign up to request clarification or add additional context in comments.

4 Comments

Of course you do not have to specify all these options.. that's just what SSMS does when you script the creation of a database.
while this would work, It is impractical in my case since I am using a parametrized Powershell script file which gets the flags as input. This would involve a lot of writing....
ok what if you just exeute this if necessary after database creation: ALTER DATABASE [test] SET ALLOW_SNAPSHOT_ISOLATION ON GO
That's also an option
1

This script demonstrates all the flags for the default database creation T-SQL in SQL 2016:

$DB=[Microsoft.SqlServer.Management.Smo.Database]::new($Server,"TemporaryDB")
# CONTAINMENT = NONE
$DB.ContainmentType=[Microsoft.SqlServer.Management.Smo.ContainmentType]::None
#ALTER DATABASE [TemporaryDB] SET COMPATIBILITY_LEVEL = 130
$DB.CompatibilityLevel=130
#ALTER DATABASE [TemporaryDB] SET ANSI_NULL_DEFAULT OFF 
$DB.AnsiNullDefault=$false
#ALTER DATABASE [TemporaryDB] SET ANSI_NULLS OFF 
$DB.AnsiNullsEnabled=$false
#ALTER DATABASE [TemporaryDB] SET ANSI_PADDING OFF 
$DB.AnsiPaddingEnabled=$false
#ALTER DATABASE [TemporaryDB] SET ANSI_WARNINGS OFF 
$DB.AnsiWarningsEnabled=$false
#ALTER DATABASE [TemporaryDB] SET ARITHABORT OFF 
$DB.ArithmeticAbortEnabled=$false
#ALTER DATABASE [TemporaryDB] SET AUTO_CLOSE OFF 
$DB.AutoClose=$false
#ALTER DATABASE [TemporaryDB] SET AUTO_SHRINK OFF 
$DB.AutoShrink=$false
#ALTER DATABASE [TemporaryDB] SET AUTO_CREATE_STATISTICS ON(INCREMENTAL = OFF)
$DB.AutoCreateStatisticsEnabled=$true
$DB.AutoCreateIncrementalStatisticsEnabled=$false
#ALTER DATABASE [TemporaryDB] SET AUTO_UPDATE_STATISTICS ON 
$DB.AutoUpdateStatisticsEnabled=$false
#ALTER DATABASE [TemporaryDB] SET CURSOR_CLOSE_ON_COMMIT OFF 
$DB.CloseCursorsOnCommitEnabled=$false
#ALTER DATABASE [TemporaryDB] SET CURSOR_DEFAULT  GLOBAL 
$DB.LocalCursorsDefault=$false
#ALTER DATABASE [TemporaryDB] SET CONCAT_NULL_YIELDS_NULL OFF 
$DB.ConcatenateNullYieldsNull=$false
#ALTER DATABASE [TemporaryDB] SET NUMERIC_ROUNDABORT OFF 
$DB.NumericRoundAbortEnabled=$false
#ALTER DATABASE [TemporaryDB] SET QUOTED_IDENTIFIER OFF 
$DB.QuotedIdentifiersEnabled=$false
#ALTER DATABASE [TemporaryDB] SET RECURSIVE_TRIGGERS OFF 
$DB.RecursiveTriggersEnabled=$false
#ALTER DATABASE [TemporaryDB] SET  DISABLE_BROKER 
$DB.BrokerEnabled=$false
#ALTER DATABASE [TemporaryDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
$DB.AutoUpdateStatisticsAsync=$false
#ALTER DATABASE [TemporaryDB] SET DATE_CORRELATION_OPTIMIZATION OFF 
$DB.DateCorrelationOptimization=$false
#ALTER DATABASE [TemporaryDB] SET PARAMETERIZATION SIMPLE 
$DB.IsParameterizationForced=$false
#ALTER DATABASE [TemporaryDB] SET READ_COMMITTED_SNAPSHOT OFF 
$DB.IsReadCommittedSnapshotOn=$false
#ALTER DATABASE [TemporaryDB] SET  READ_WRITE 
$DB.ReadOnly=$false
#ALTER DATABASE [TemporaryDB] SET RECOVERY FULL 
$DB.RecoveryModel=[Microsoft.SqlServer.Management.Smo.RecoveryModel]::Full
#ALTER DATABASE [TemporaryDB] SET  MULTI_USER 
$DB.UserAccess=[Microsoft.SqlServer.Management.Smo.DatabaseUserAccess]::Multiple
#ALTER DATABASE [TemporaryDB] SET PAGE_VERIFY CHECKSUM  
$DB.PageVerify=[Microsoft.SqlServer.Management.Smo.PageVerify]::Checksum
#ALTER DATABASE [TemporaryDB] SET TARGET_RECOVERY_TIME = 60 SECONDS 
$DB.TargetRecoveryTime=60
#ALTER DATABASE [TemporaryDB] SET DELAYED_DURABILITY = DISABLED 
$DB.DelayedDurability=[Microsoft.SqlServer.Management.Smo.DelayedDurability]::Disabled
#ALTER DATABASE SCOPED CONFIGURATION SET MAXDOP = 0;
$DB.MaxDop=0
#ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET MAXDOP = PRIMARY;
$DB.MaxDopForSecondary=[Microsoft.SqlServer.Management.Smo.DatabaseScopedConfigurationOnOff]::Primary
#ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = OFF;
$DB.LegacyCardinalityEstimation=[Microsoft.SqlServer.Management.Smo.DatabaseScopedConfigurationOnOff]::Off
#ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET LEGACY_CARDINALITY_ESTIMATION = PRIMARY;
$DB.LegacyCardinalityEstimationForSecondary=[Microsoft.SqlServer.Management.Smo.DatabaseScopedConfigurationOnOff]::Primary
#ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SNIFFING = ON;
$DB.ParameterSniffing=[Microsoft.SqlServer.Management.Smo.DatabaseScopedConfigurationOnOff]::On
#ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET PARAMETER_SNIFFING = PRIMARY;
$DB.ParameterSniffingForSecondary=[Microsoft.SqlServer.Management.Smo.DatabaseScopedConfigurationOnOff]::Primary
#ALTER DATABASE SCOPED CONFIGURATION SET QUERY_OPTIMIZER_HOTFIXES = OFF;
$DB.QueryOptimizerHotfixes=[Microsoft.SqlServer.Management.Smo.DatabaseScopedConfigurationOnOff]::Off
#ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET QUERY_OPTIMIZER_HOTFIXES = PRIMARY;
$DB.QueryOptimizerHotfixesForSecondary=[Microsoft.SqlServer.Management.Smo.DatabaseScopedConfigurationOnOff]::Primary

Comments

0

Turns out I can just call $Db.SomeFlag = $someValue and that works, for example:
$Db.AutoShrink = $True

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.