I initially found this message looking for a solution to copy stored procedures from my remote production database to my local development database. After success, using the proposed approach in this topic, I realized that I was becoming more and more lazy (or resourceful, depending on what you prefer) and wanted this to be automated. I came across this link , which turned out to be very useful (thanks vincpa), and I expanded it, resulting in the following file (schema_backup.ps1):
$server = "servername" $database = "databaseName" $output_path = "D:\prod_schema_backup" $login = "username" $password = "password" $schema = "dbo" $table_path = "$output_path\table\" $storedProcs_path = "$output_path\stp\" $views_path = "$output_path\view\" $udfs_path = "$output_path\udf\" $textCatalog_path = "$output_path\fulltextcat\" $udtts_path = "$output_path\udtt\" [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | out-null [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | out-null $srvConn = new-object Microsoft.SqlServer.Management.Common.ServerConnection $srvConn.ServerInstance = $server $srvConn.LoginSecure = $false $srvConn.Login = $login $srvConn.Password = $password $srv = New-Object Microsoft.SqlServer.Management.SMO.Server($srvConn) $db = New-Object ("Microsoft.SqlServer.Management.SMO.Database") $tbl = New-Object ("Microsoft.SqlServer.Management.SMO.Table") $scripter = New-Object Microsoft.SqlServer.Management.SMO.Scripter($srvConn) # Get the database and table objects $db = $srv.Databases[$database] $tbl = $db.tables | Where-object { $_.schema -eq $schema -and -not $_.IsSystemObject } $storedProcs = $db.StoredProcedures | Where-object { $_.schema -eq $schema -and -not $_.IsSystemObject } $views = $db.Views | Where-object { $_.schema -eq $schema } $udfs = $db.UserDefinedFunctions | Where-object { $_.schema -eq $schema -and -not $_.IsSystemObject } $catlog = $db.FullTextCatalogs $udtts = $db.UserDefinedTableTypes | Where-object { $_.schema -eq $schema } # Set scripter options to ensure only data is scripted $scripter.Options.ScriptSchema = $true; $scripter.Options.ScriptData = $false; #Exclude GOs after every line $scripter.Options.NoCommandTerminator = $false; $scripter.Options.ToFileOnly = $true $scripter.Options.AllowSystemObjects = $false $scripter.Options.Permissions = $true $scripter.Options.DriAllConstraints = $true $scripter.Options.SchemaQualify = $true $scripter.Options.AnsiFile = $true $scripter.Options.SchemaQualifyForeignKeysReferences = $true $scripter.Options.Indexes = $true $scripter.Options.DriIndexes = $true $scripter.Options.DriClustered = $true $scripter.Options.DriNonClustered = $true $scripter.Options.NonClusteredIndexes = $true $scripter.Options.ClusteredIndexes = $true $scripter.Options.FullTextIndexes = $true $scripter.Options.EnforceScriptingOptions = $true function CopyObjectsToFiles($objects, $outDir) { #clear out before Remove-Item $outDir* -Force -Recurse if (-not (Test-Path $outDir)) { [System.IO.Directory]::CreateDirectory($outDir) } foreach ($o in $objects) { if ($o -ne $null) { $schemaPrefix = "" if ($o.Schema -ne $null -and $o.Schema -ne "") { $schemaPrefix = $o.Schema + "." } #removed the next line so I can use the filename to drop the stored proc #on the destination and recreate it #$scripter.Options.FileName = $outDir + $schemaPrefix + $o.Name + ".sql" $scripter.Options.FileName = $outDir + $schemaPrefix + $o.Name Write-Host "Writing " $scripter.Options.FileName $scripter.EnumScript($o) } } } # Output the scripts CopyObjectsToFiles $tbl $table_path CopyObjectsToFiles $storedProcs $storedProcs_path CopyObjectsToFiles $views $views_path CopyObjectsToFiles $catlog $textCatalog_path CopyObjectsToFiles $udtts $udtts_path CopyObjectsToFiles $udfs $udfs_path Write-Host "Finished at" (Get-Date) $srv.ConnectionContext.Disconnect()
I have a .bat file that calls this and is called from the task scheduler. After calling the Powershell file, I have:
for /f %f in ('dir /bd:\prod_schema_backup\stp\') do sqlcmd /S localhost /d dest_db /Q "DROP PROCEDURE %f"
This line will go through the directory and discard the procedures that it is going to recreate. If this were not a development environment, I would not want software to be discarded in this way. Then I will rename all stored procedure files to have .sql:
powershell Dir d:\prod_schema_backup\stp\ | Rename-Item -NewName { $_.name + ".sql" }
And then run:
for /f %f in ('dir /bd:\prod_schema_backup\stp\') do sqlcmd /S localhost /d dest_db /E /i "%f".sql
And it repeats through all .sql files and recreates stored procedures. I hope that any part of this will prove useful to someone.
vandsh Feb 20 '14 at 19:53 2014-02-20 19:53
source share