服务器支持最存储过程,SQL服务器 - 复制存储过程从一个分贝另一个

这篇博客分享了一个PowerShell脚本,用于从远程生产数据库复制存储过程到本地开发数据库。脚本包含了连接数据库、筛选特定模式的对象、设置脚本选项、以及将对象导出为单独文件的逻辑。之后通过批处理文件删除并重新创建存储过程。此自动化流程旨在提高开发效率。
摘要由CSDN通过智能技术生成

我最初发现这个帖子寻找一个解决方案,复制从我的远程生产数据库的存储过程,以我的本地开发数据库。在这个线程中成功使用了建议的方法之后,我意识到我越来越懒惰(或者足智多谋,无论你喜欢什么),并希望自动化。我碰到this link,这被证明是非常有帮助的来到(谢谢vincpa),我在扩展它,导致以下文件(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()

我有一个调用这个.bat文件,并从任务计划程序调用。在调用Powershell的文件后,我有:

for /f %f in ('dir /b d:\prod_schema_backup\stp\') do sqlcmd /S localhost /d dest_db /Q "DROP PROCEDURE %f"

该行将走通的目录,并把它打算重新创建的过程。如果这不是一个开发环境,我不会喜欢以这种方式编程式地删除过程。然后我重命名所有的存储过程文件。SQL:

powershell Dir d:\prod_schema_backup\stp\ | Rename-Item -NewName { $_.name + ".sql" }

然后运行:

for /f %f in ('dir /b d:\prod_schema_backup\stp\') do sqlcmd /S localhost /d dest_db /E /i "%f".sql

这遍历所有的.sql文件,并重新创建存储过程。我希望这个任何部分都能证明对某人有帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值