Creating an index
This recipe demonstrates how to create a non-clustered index with an included column using PowerShell and SMO.
Getting ready
We will use
the Person.Person
table in the AdventureWorks2008R2
database. We will create a non-clustered index on FirstName
, LastName
, and include MiddleName
. The T-SQL equivalent of this task is:
CREATE NONCLUSTERED INDEX [idxLastNameFirstName] ON [Person].[Person] ( [LastName] ASC, [FirstName] ASC ) INCLUDE ( [MiddleName]) GO
How to do it...
Open the PowerShell console by going to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
Import the
SQLPS
module, and create a new SMO Server object:#import SQL Server module Import-Module SQLPS -DisableNameChecking #replace this with your instance name $instanceName = "KERRIGAN" $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
Add the following script and run it:
$dbName = "AdventureWorks2008R2" $db = $server.Databases[$dbName] $tableName...