我希望能够在更改它之前以.sql
文件的形式创建SQL Server存储过程的备份。有没有一种将存储过程保存到文件夹的方法?任何帮助表示赞赏
快速的网络搜索,使用帖子的标题/上下文,大部分时间都可以为您提供所需的一切,但是足以使您走上所需的道路。
PowerShell 'backup SQL stored procedure'
Using Powershell to backup your stored procedures and triggers
以下PowerShell脚本连接到映射的驱动器并写入目标系统上每个数据库的所有存储过程和触发器到单个文件。
代码:
$rootDrive = "H:"
#the full path of the file that you want to script the stored procedures to
$strDate = (get-Date).tostring("yyyyMMddHHssmm")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
$MyScripter=new-object ("Microsoft.SqlServer.Management.Smo.Scripter")
$srv=New-Object "Microsoft.SqlServer.Management.Smo.Server" "localhost"
foreach($sqlDatabase in $srv.databases)
{
$procs = $sqlDatabase.StoredProcedures
$views = $sqlDatabase.views
$tables = $sqlDatabase.tables
$udfs = $sqlDatabase.UserDefinedFunctions
$sqlDatabaseName = $sqlDatabase.name
$MyScripter.Server=$srv
"************* $sqlDatabaseName"
#STORED PROCEDURES
if($procs -ne $null)
{
foreach ($proc in $procs)
{
#Assuming that all non-system stored procedures have proper naming convention and don't use prefixes like "sp_"
if ( $proc.Name.IndexOf("sp_") -eq -1 -and $proc.Name.IndexOf("xp_") -eq -1 -and $proc.Name.IndexOf("dt_") -eq -1)
{
$fileName = $proc.name
"Scripting SP $fileName"
$scriptfile = "$rootDrive\DatabaseScripts\$sqlDatabaseName\$strDate\StoredProcedures\$filename.sql"
New-Item $rootDrive\DatabaseScripts -type directory -force | out-null
New-Item $rootDrive\DatabaseScripts\$sqlDatabaseName -type directory -force | out-null
New-Item $rootDrive\DatabaseScripts\$sqlDatabaseName\$strDate -type directory -force | out-null
New-Item $rootDrive\DatabaseScripts\$sqlDatabaseName\$strDate\StoredProcedures -type directory -force | out-null
$MyScripter.Options.FileName = $scriptfile
#AppendTofile has to be 'true' in order that all the procs' scripts will be appended at the end
$MyScripter.Options.AppendToFile = "true"
$MyScripter.Script($proc)|out-null
}
}
}
#VIEWS
if($views -ne $null)
{
foreach ($view in $views)
{
#Only script views that are properly named
if ( $view.Name.IndexOf("View") -eq 0)
{
$fileName = $view.name
"Scripting View $fileName"
$scriptfile = "$rootDrive\DatabaseScripts\$sqlDatabaseName\$strDate\Views\$fileName.sql"
New-Item $rootDrive\DatabaseScripts -type directory -force | out-null
New-Item $rootDrive\DatabaseScripts\$sqlDatabaseName -type directory -force | out-null
New-Item $rootDrive\DatabaseScripts\$sqlDatabaseName\$strDate -type directory -force | out-null
New-Item $rootDrive\DatabaseScripts\$sqlDatabaseName\$strDate\Views -type directory -force | out-null
$MyScripter.Options.FileName = $scriptfile
#AppendTofile has to be 'true' in order that all the procs' scripts will be appended at the end
$MyScripter.Options.AppendToFile = "true"
$MyScripter.Script($view)|out-null
}
}
}
#TABLES
if($tables -ne $null)
{
$scriptfile = "$rootDrive\DatabaseScripts\$sqlDatabaseName\$strDate\AllTables.sql"
New-Item $rootDrive\DatabaseScripts -type directory -force | out-null
New-Item $rootDrive\DatabaseScripts\$sqlDatabaseName -type directory -force | out-null
New-Item $rootDrive\DatabaseScripts\$sqlDatabaseName\$strDate -type directory -force | out-null
$MyScripter.Options.FileName = $scriptfile
#AppendTofile has to be 'true' in order that all the procs' scripts will be appended at the end
"Scripting out creation script for all tables in $sqlDatabasename"
$MyScripter.Options.AppendToFile = "true"
$MyScripter.Script($tables)|out-null
foreach ($table in $tables)
{
$tableName = $table.name
#TRIGGERS
if($table.triggers -ne $null)
{
foreach ($trigger in $table.triggers)
{
$fileName = $trigger.name
"Scripting trigger $fileName"
$scriptfile = "$rootDrive\DatabaseScripts\$sqlDatabaseName\$strDate\Triggers\$fileName.sql"
New-Item $rootDrive\DatabaseScripts -type directory -force | out-null
New-Item $rootDrive\DatabaseScripts\$sqlDatabaseName -type directory -force | out-null
New-Item $rootDrive\DatabaseScripts\$sqlDatabaseName\$strDate -type directory -force | out-null
New-Item $rootDrive\DatabaseScripts\$sqlDatabaseName\$strDate\Triggers -type directory -force | out-null
$MyScripter.Options.FileName = $scriptfile
#AppendTofile has to be 'true' in order that all the procs' scripts will be appended at the end
$MyScripter.Options.AppendToFile = "true"
$MyScripter.Script($trigger)|out-null
}
}
}
}
#USER DEFINED FUNCTIONS
if($udfs -ne $null)
{
foreach ($udf in $udfs)
{
if ( $udf.Name.IndexOf("dm_") -eq -1 -and $udf.Name.IndexOf("fn_") -eq -1)
{
$fileName = $udf.name
"Scripting UDF $fileName"
$scriptfile = "$rootDrive\DatabaseScripts\$sqlDatabaseName\$strDate\UDFs\$fileName.sql"
New-Item $rootDrive\DatabaseScripts -type directory -force | out-null
New-Item $rootDrive\DatabaseScripts\$sqlDatabaseName -type directory -force | out-null
New-Item $rootDrive\DatabaseScripts\$sqlDatabaseName\$strDate -type directory -force | out-null
New-Item $rootDrive\DatabaseScripts\$sqlDatabaseName\$strDate\UDFs -type directory -force | out-null
$MyScripter.Options.FileName = $scriptfile
#AppendTofile has to be 'true' in order that all the procs' scripts will be appended at the end
$MyScripter.Options.AppendToFile = "true"
$MyScripter.Script($udf)|out-null
}
}
}
}
另请参见: