Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <a href="http://msdn.microsoft.com/en-us/library/ms162169.aspx" rel="noreferrer">sql smo</a> for basically implementing all functionality available in SQL Server Enterprise manager. There is a nice tutorial <a href="http://www.mssqltips.com/sqlservertip/1833/generate-scripts-for-database-objects-with-smo-for-sql-server/" rel="noreferrer">here</a>. </p> <p><strong>Edit</strong>: an example using <a href="http://msdn.microsoft.com/en-us/library/ms162552%28v=sql.105%29.aspx" rel="noreferrer">SMO</a> in PowerShell</p> <pre><code>function SQL-Script-Database { &lt;# .SYNOPSIS Script all database objects for the given database. .DESCRIPTION This function scripts all database objects (i.e.: tables, views, stored procedures, and user defined functions) for the specified database on the the given server\instance. It creates a subdirectory per object type under the path specified. .PARAMETER savePath The root path where to save object definitions. .PARAMETER database The database to script (default = $global:DatabaseName) .PARAMETER DatabaseServer The database server to be used (default: $global:DatabaseServer). .PARAMETER InstanceName The instance name to be used (default: $global:InstanceName). .EXAMPLE SQL-Script-Database c:\temp AOIDB #&gt; param ( [parameter(Mandatory = $true)][string] $savePath, [parameter(Mandatory = $false)][string] $database = $global:DatabaseName, [parameter(Mandatory = $false)][string] $DatabaseServer = $global:DatabaseServer, [parameter(Mandatory = $false)][string] $InstanceName = $global:InstanceName ) try { if (!$DatabaseServer -or !$InstanceName) { throw "`$DatabaseServer or `$InstanceName variable is not properly initialized" } $ServerInstance = SQL-Get-Server-Instance $DatabaseServer $InstanceName [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null $s = New-Object Microsoft.SqlServer.Management.Smo.Server($ServerInstance) $db = $s.databases[$database] $objects = $db.Tables $objects += $db.Views $objects += $db.StoredProcedures $objects += $db.UserDefinedFunctions $scripter = New-Object ('Microsoft.SqlServer.Management.Smo.Scripter') ($s) $scripter.Options.AnsiFile = $true $scripter.Options.IncludeHeaders = $false $scripter.Options.ScriptOwner = $false $scripter.Options.AppendToFile = $false $scripter.Options.AllowSystemobjects = $false $scripter.Options.ScriptDrops = $false $scripter.Options.WithDependencies = $false $scripter.Options.SchemaQualify = $false $scripter.Options.SchemaQualifyForeignKeysReferences = $false $scripter.Options.ScriptBatchTerminator = $false $scripter.Options.Indexes = $true $scripter.Options.ClusteredIndexes = $true $scripter.Options.NonClusteredIndexes = $true $scripter.Options.NoCollation = $true $scripter.Options.DriAll = $true $scripter.Options.DriIncludeSystemNames = $false $scripter.Options.ToFileOnly = $true $scripter.Options.Permissions = $true foreach ($o in $objects | where {!($_.IsSystemObject)}) { $typeFolder=$o.GetType().Name if (!(Test-Path -Path "$savepath\$typeFolder")) { New-Item -Type Directory -name "$typeFolder"-path "$savePath" | Out-Null } $file = $o -replace "\[|\]" $file = $file.Replace("dbo.", "") $scripter.Options.FileName = "$savePath\$typeFolder\$file.sql" $scripter.Script($o) } } catch { Util-Log-Error "`t`t$($MyInvocation.InvocationName): $_" } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload