Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 9th, 2012  |  syntax: None  |  size: 2.93 KB  |  hits: 5  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. $server                         = "localhost"
  2. $database                       = "NerdDinner"
  3. $output_path            = "C:\dev\nerddinner\Schema"
  4.  
  5. $schema                         = "dbo"
  6. $table_path             = "$output_path\Table\"
  7. $storedProcs_path       = "$output_path\StoredProcedure\"
  8. $views_path             = "$output_path\View\"
  9. $udfs_path                      = "$output_path\UserDefinedFunction\"
  10. $textCatalog_path       = "$output_path\FullTextCatalog\"
  11. $udtts_path             = "$output_path\UserDefinedTableTypes\"
  12.  
  13. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
  14.  
  15. $srv            = New-Object "Microsoft.SqlServer.Management.SMO.Server" $server
  16. $db             = New-Object ("Microsoft.SqlServer.Management.SMO.Database")
  17. $tbl            = New-Object ("Microsoft.SqlServer.Management.SMO.Table")
  18. $scripter       = New-Object ("Microsoft.SqlServer.Management.SMO.Scripter") ($server)
  19.  
  20. # Get the database and table objects
  21. $db = $srv.Databases[$database]
  22.  
  23. $tbl             = $db.tables | Where-object { $_.schema -eq $schema  -and -not $_.IsSystemObject }
  24. $storedProcs     = $db.StoredProcedures | Where-object { $_.schema -eq $schema -and -not $_.IsSystemObject }
  25. $views           = $db.Views | Where-object { $_.schema -eq $schema }
  26. $udfs            = $db.UserDefinedFunctions | Where-object { $_.schema -eq $schema -and -not $_.IsSystemObject }
  27. $catlog          = $db.FullTextCatalogs
  28. $udtts           = $db.UserDefinedTableTypes | Where-object { $_.schema -eq $schema }
  29.  
  30. # Set scripter options to ensure only data is scripted
  31. $scripter.Options.ScriptSchema  = $true;
  32. $scripter.Options.ScriptData    = $false;
  33.  
  34. #Exclude GOs after every line
  35. $scripter.Options.NoCommandTerminator   = $false;
  36. $scripter.Options.ToFileOnly            = $true
  37. $scripter.Options.AllowSystemObjects    = $false
  38. $scripter.Options.Permissions           = $true
  39. $scripter.Options.DriAllConstraints     = $true
  40. $scripter.Options.SchemaQualify         = $true
  41. $scripter.Options.AnsiFile              = $true
  42.  
  43. $scripter.Options.SchemaQualifyForeignKeysReferences = $true
  44.  
  45. $scripter.Options.Indexes                       = $true
  46. $scripter.Options.DriIndexes                    = $true
  47. $scripter.Options.DriClustered                  = $true
  48. $scripter.Options.DriNonClustered               = $true
  49. $scripter.Options.NonClusteredIndexes           = $true
  50. $scripter.Options.ClusteredIndexes              = $true
  51. $scripter.Options.FullTextIndexes               = $true
  52.  
  53. $scripter.Options.EnforceScriptingOptions       = $true
  54.  
  55. function CopyObjectsToFiles($objects, $outDir) {
  56.        
  57.         if (-not (Test-Path $outDir)) {
  58.                 [System.IO.Directory]::CreateDirectory($outDir)
  59.         }
  60.        
  61.         foreach ($o in $objects) {
  62.        
  63.                 if ($o -ne $null) {
  64.                        
  65.                         $schemaPrefix = ""
  66.                        
  67.                         if ($o.Schema -ne $null -and $o.Schema -ne "") {
  68.                                 $schemaPrefix = $o.Schema + "."
  69.                         }
  70.                
  71.                         $scripter.Options.FileName = $outDir + $schemaPrefix + $o.Name + ".sql"
  72.                         Write-Host "Writing " $scripter.Options.FileName
  73.                         $scripter.EnumScript($o)
  74.                 }
  75.         }
  76. }
  77.  
  78. # Output the scripts
  79. CopyObjectsToFiles $tbl $table_path
  80. CopyObjectsToFiles $storedProcs $storedProcs_path
  81. CopyObjectsToFiles $views $views_path
  82. CopyObjectsToFiles $catlog $textCatalog_path
  83. CopyObjectsToFiles $udtts $udtts_path
  84. CopyObjectsToFiles $udfs $udfs_path
  85.  
  86. Write-Host "Finished at" (Get-Date)