CreateTable-MSSQL-UsingSMO


posted by Richard Giles
10-06-2008

Downloads: 483
File size: 4.1kB
Views: 2,203

Embed
CreateTable-MSSQL-UsingSMO
  1. ## ===================================================================== 
  2. ## Title       : CreateTable-MSSQL-UsingSMO 
  3. ## Description : Create a new table using SMO 
  4. ## Author      : Idera 
  5. ## Date        : 6/27/2008 
  6. ## Input       : -serverInstance <server\instance> 
  7. ##                   -dbName <database name> 
  8. ##                   -schemaName <schema name> 
  9. ##                   -tblName <table name> 
  10. ##                   -verbose  
  11. ##                   -debug     
  12. ## Output      : Create a demo database and table 
  13. ## Usage            : PS> . CreateTable-MSSQL-UsingSMO -serverInstance MyServer -dbName SMOTestDB  
  14. ##                                             -SchemaName SMOSchema -tblName SMOTable -v -d 
  15. ## Notes            : Adapted from Allen White script 
  16. ## Tag            : SMO, SQL Server, Table 
  17. ## Change log  : 
  18. ## ===================================================================== 
  19.  
  20. param 
  21.       [string]$serverInstance = "(local)"
  22.     [string]$dbName = "SMOTestDB"
  23.     [string]$schemaName = "SMOSchema"
  24.     [string]$tblName = "SMOTable"
  25.     [switch]$verbose
  26.     [switch]$debug 
  27.  
  28. function main() 
  29.     if ($verbose) {$VerbosePreference = "Continue"
  30.     if ($debug) {$DebugPreference = "Continue"
  31.     CreateTable-MSSQL-UsingSMO $serverInstance $dbName $schemaName $tblName 
  32.  
  33. function CreateTable-MSSQL-UsingSMO($serverInstance, $dbName, $schemaName, $tblName
  34.     trap [Exception]  
  35.     
  36.         write-error $("TRAPPED: " + $_.Exception.Message); 
  37.         continue
  38.     
  39.      
  40.     # Load SMO assemblies 
  41.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo"
  42.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlEnum"
  43.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum"
  44.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo"
  45.      
  46.     # Instantiate a server object for the default instance 
  47.     $namedInstance = New-Object ('Microsoft.SqlServer.Management.Smo.Server') ($serverInstance
  48.      
  49.     # Disable connection pooling 
  50.     $namedInstance.ConnectionContext.Set_NonPooledConnection($TRUE
  51.      
  52.     # Explicitly connect because connection pooling is disabled 
  53.     $namedInstance.ConnectionContext.Connect() 
  54.  
  55.     cls 
  56.      
  57.     # Connect to the server with Windows Authentication and drop database if exist 
  58.     if ($namedInstance.Databases[$dbName] -ne $null)  
  59.     
  60.         Write-Debug "The test database already exists on $namedInstance" 
  61.         Write-Debug "Deleting it now..." 
  62.         $namedInstance.Databases[$dbName].drop() 
  63.     
  64.  
  65.     # Instantiate a database object 
  66.     Write-Debug "Creating database: $dbName" 
  67.     $database = new-object("Microsoft.SqlServer.Management.Smo.Database") ($namedInstance, $dbName
  68.      
  69.     # Create the new database on the server 
  70.     $database.Create() 
  71.      
  72.     # Instantiate a schema object  
  73.     Write-Debug "Creating schema: $SchemaName" 
  74.     $schema = new-object("Microsoft.SqlServer.Management.Smo.Schema") ($database, $schemaName
  75.      
  76.     # Create the new schema on the server 
  77.     $schema.Create() 
  78.      
  79.     # Instantiate a table object 
  80.     Write-Debug "Creating table: $TblName" 
  81.     $table = new-object("Microsoft.SqlServer.Management.Smo.Table") ($Database, $TblName
  82.      
  83.     # Add Field1 column  
  84.     $colField1 = New-Object("Microsoft.SqlServer.Management.Smo.Column") ($table, "Field1"
  85.     # TIP: setting an object type from a NetClassStaticMethod 
  86.     $colField1.DataType = [Microsoft.SqlServer.Management.Smo.Datatype]::Int 
  87.     $table.Columns.Add($colField1
  88.      
  89.     # Add Field2 column 
  90.     $colField2 = New-Object("Microsoft.SqlServer.Management.Smo.Column") ($table, "Field2"
  91.     $colField2.DataType =  [Microsoft.SqlServer.Management.Smo.Datatype]::NVarchar(25) 
  92.     $table.Columns.Add($colField2
  93.      
  94.     # Add Field3 column 
  95.     $colField3 = New-Object("Microsoft.SqlServer.Management.Smo.Column") ($table, "Field3"
  96.     $colField3.DataType = [Microsoft.SqlServer.Management.Smo.Datatype]::NVarchar(50) 
  97.     $Table.Columns.Add($colField3
  98.      
  99.     # Set the schema property of the table.  
  100.     $table.Schema = $schemaName 
  101.      
  102.     # Create the table on the server 
  103.     $table.Create() 
  104.      
  105.     Write-Host "Table: $dbName.$schemaName.$tblName created" 
  106.      
  107.     # Explicitly disconnect because connection pooling is disabled 
  108.     Write-Debug "Disconnecting..." 
  109.     $namedInstance.ConnectionContext.Disconnect() 
  110.  
  111. main 
Filed under: , ,

Create a SQL Server table using SMO.

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.