Databases Reference
In-Depth Information
$datatype0 = [Microsoft.SqlServer.Management.Smo.Datatype]::datetime
$datatype1 = [Microsoft.SqlServer.Management.Smo.Datatype]::int
$datatype2 = [Microsoft.SqlServer.Management.Smo.Datatype]::Money
$datatype3 = [Microsoft.SqlServer.Management.Smo.Datatype]::Nchar(50)
$col0 = New-Object 'Microsoft.SqlServer.Management.Smo.Column'
$col0.Name = "DOB"
$table.Columns.Add($col0)
$col0.Nullable = $false
$col0.Datatype = $datatype0
$col1 = New-Object 'Microsoft.SqlServer.Management.Smo.Column'
$col1.Name = "ID"
$table.Columns.Add($col1)
$col1.Nullable = $false
$col1.Datatype = $datatype1
$col2 = New-Object 'Microsoft.SqlServer.Management.Smo.Column'
$col2.Name = "Salary"
$table.Columns.Add($col2)
$col2.Nullable = $false
$col2.Datatype = $datatype2
$col3 = New-Object 'Microsoft.SqlServer.Management.Smo.Column'
$col3.Name = "Address"
$table.Columns.Add($col3)
$col3.Nullable = $false
$col3.Datatype = $datatype3
$table.alter()
Dropping a Table
SMO can also be used to drop existing tables. Execute the cmdlets as shown here (see Figure 13-36):
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") |
Out-Null
$srv = New-Object ('Microsoft.SqlServer.Management.Smo.Server')
'PowerServer3\SQL2008'
$db = $srv.Databases["MyDBTest"]
$table = $db.Tables["MyTable"]
$table.drop()
Backup and Restore with SMO
SMO can also be used to back up and restore a database.
Database Backup
As you already know, there are three different backups — namely, full backup, differential, and
transactional log backup. SMO can be used to take backups of all databases as well. Earlier in this
Search WWH ::




Custom Search