关于编写对象的脚本,您可以通过 DMO、SMO、PowerShell、VBScript 和任何其他数量的方法来完成。我仍然使用我编写的古老的 VB DMO 脚本,该脚本基于来自多个网站的代码,并通过 SSIS 脚本任务执行代码。设置执行包的 SQL Server 代理作业并添加签入代码的步骤。我不熟悉 TFS,但我已经使用 Visual SourceSafe 和 Perforce 完成了这项工作,没有任何问题。诀窍是您只需要学习如何进行无人值守的命令行签入来合并更改(更新、删除、创建)。下面是我古老的 VB.NET 脚本任务。请记住,您可以将其中的一部分替换为变量以使其更具动态性。
' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic 2008.
' The ScriptMain is the entry point class of the script.
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.IO
Imports System.Text.RegularExpressions
<System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
Partial Class ScriptMain
Private Sub ScriptMain_Startup(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
Private Sub ScriptMain_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs)
Try
' Unlock variables from the read-only and read-write variable collection properties
If (Dts.Variables.Count <> 0) Then
Dts.Variables.Unlock()
End If
Catch ex As Exception
End Try
End Sub
Enum ScriptResults
Success = DTSExecResult.Success
Failure = DTSExecResult.Failure
End Enum
Private Function CleanFileName(ByVal FileName As String) As String
FileName = Regex.Replace(FileName, "[/\\:?=*<>]", "-")
Return FileName
End Function
Public Sub Main()
''THESE MUST BE SET AND THE AGENT ACCOUNT MUST HAVE WRITE ACCESS TO THE DRIVE SELECTED
Dim FolderName = "\\Your\Folder\Hierarchy\"
Dim ServerName = "your-server-name"
Dim ScriptOptions As Integer
ScriptOptions = 1 ' Generate Drop
ScriptOptions = ScriptOptions Or 4 ' Generate Default (Create)
ScriptOptions = ScriptOptions Or 34 ' Generate Permissions (Database & Object)
Dim ScriptOptions_Jobs As Integer
ScriptOptions_Jobs = 1 ' Generate Drop
ScriptOptions_Jobs = ScriptOptions_Jobs Or 1203765415 ' Other stuff
Dim ScriptOptions_Tables As Integer
ScriptOptions_Tables = 1 ' Generate Drop
ScriptOptions_Tables = ScriptOptions_Tables Or 4 ' Generate Default (Create)
ScriptOptions_Tables = ScriptOptions_Tables Or 34 ' Generate Permissions (Database & Object)
ScriptOptions_Tables = ScriptOptions_Tables Or 73736 ' Generate Indexes
Dim EXCLUDE_LIST = "master/tempdb/model/msdb"
''Standardize the end of the folder name to include \.
If Right(FolderName, 1) <> "\" Then
FolderName = FolderName & "\"
End If
''Declare a folder that can be deleted. Delete doesn't like ending with \.
Dim FolderNameDelete = FolderName
If Right(FolderNameDelete, 1) = "\" Then FolderNameDelete = _
Left(FolderNameDelete, Len(FolderNameDelete) - 1)
'Used for file system tasks.
Dim oFSO = CreateObject("Scripting.FileSystemObject")
'Delete if the folder exists. If you don't delete the folder and populate the scripts,
'then objects that were deleted from the database won't disappear and the Perforce diff
'won't recognize that the objects need to be deleted in Perforce.
If oFSO.FolderExists(FolderName) = True Then
oFSO.DeleteFolder(FolderNameDelete, True)
End If
'Create the folder now so that we have a clean script destination.
oFSO.CreateFolder(FolderName)
oFSO.CreateFolder(FolderName & "Databases")
oFSO.CreateFolder(FolderName & "Jobs")
'connect to the server instance using trusted credentials so we dont have password stored in a file and we dont have
'to worry about password changes breaking anything
Dim oServer = CreateObject("SQLDMO.SQLServer")
With oServer
.LoginSecure = True
.Connect(ServerName)
End With
'Script out SQL Server Agent Jobs.
For Each oJob In oServer.JobServer.Jobs
oJob.Script(ScriptOptions_Jobs, FolderName & "Jobs\" & CleanFileName(oJob.Name) & ".sql")
Next
'loop through all databases and excluding those in the EXCLUDE_LIST above, script out all the stored procedures.
'You could easily change this to read from a table or to let you pass in a different list.
For Each oDB In oServer.Databases
If InStr(1, EXCLUDE_LIST, oDB.Name) = 0 Then
'each db will get a folder in the main folder (FolderName) that will act as a container for the backup
'folders we'll create each time we run this
Dim dbFolder = FolderName & "Databases\" & oDB.Name & "\"
oFSO.CreateFolder(dbFolder)
oFSO.CreateFolder(dbFolder & "Stored Procedures")
oFSO.CreateFolder(dbFolder & "Tables")
oFSO.CreateFolder(dbFolder & "User-Defined Functions")
oFSO.CreateFolder(dbFolder & "Views")
'script out all the non-system procs
For Each oProc In oDB.StoredProcedures
If oProc.SystemObject = False Then
''262150= SQLDMOScript_ObjectPermissions + SQLDMOScript_PrimaryObject + SQLDMOScript_OwnerQualify
oProc.Script(ScriptOptions, dbFolder & "\Stored Procedures\" & CleanFileName(oProc.Owner) & "_" & CleanFileName(oProc.Name) & ".sql")
End If
Next
'script out all the non-system views
For Each oView In oDB.Views
If oView.SystemObject = False Then
''262150= SQLDMOScript_ObjectPermissions + SQLDMOScript_PrimaryObject + SQLDMOScript_OwnerQualify
oView.Script(ScriptOptions, dbFolder & "\Views\" & CleanFileName(oView.Owner) & "_" & CleanFileName(oView.Name) & ".sql")
End If
Next
'script out all the non-system user-defined functions
For Each oUDF In oDB.UserDefinedFunctions
If oUDF.SystemObject = False Then
''262150= SQLDMOScript_ObjectPermissions + SQLDMOScript_PrimaryObject + SQLDMOScript_OwnerQualify
oUDF.Script(ScriptOptions, dbFolder & "\User-Defined Functions\" & CleanFileName(oUDF.Owner) & "." & CleanFileName(oUDF.Name) & ".sql")
End If
Next
'script out all the non-system tables
For Each oTable In oDB.Tables
If oTable.SystemObject = False Then
''262150= SQLDMOScript_ObjectPermissions + SQLDMOScript_PrimaryObject + SQLDMOScript_OwnerQualify
oTable.Script(ScriptOptions_Tables, dbFolder & "\Tables\" & CleanFileName(oTable.Owner) & "_" & CleanFileName(oTable.Name) & ".sql")
End If
Next
End If
Next
'close it all out
oServer.DisConnect()
oServer = Nothing
oFSO = Nothing
Dts.TaskResult = ScriptResults.Success
End Sub
结束类