Sunday, March 11, 2007

Script Objects SS2K

Well, there are still lots of people/businesses use SQL Server 2000 I'd like to put the following script using SQL DMO object library. You have to open a new project with VB6 and put the code in the new form.
The cool thing is that you get the script without default COLLATION to be specify.
See if you need to run the script on another server/s or database/s that have different collation.

Also, Tibor Karaszi has great info at his web site
http://www.karaszi.com/SQLServer/info_generate_script.asp


Sub ScriptDB(strLogin As String, strPwd As String, _
strDataBase As String, StrFilePath As String)


Dim sql As Object
Dim db As Object
Dim objTrigger As Object
Dim intOptions As Long
Dim genObj

Set sql = CreateObject("SQLDMO.SQLServer")
Set db = CreateObject("SQLDMO.Database")
Set objTrigger = CreateObject("SQLDMO.Trigger")

Const sDrops As Integer = 1
Const sIncludeHeaders As Long = 131072
Const sDefault As Integer = 4
Const sAppendToFile As Integer = 256
Const sBindings As Integer = 128
' No collation to be specify
Const SQLDMOScript2_NoCollation As Long = 8388608

' We need to specify multiple behaviors
' for the ScriptType argument, you use "Or" to combine these.
intOptions = sDrops Or sIncludeHeaders Or _
sDefault Or sAppendToFile Or sBindings Or SQLDMOScript2_NoCollation

' Connect to server
sql.Connect "(local)", strLogin, strPwd
Set db = sql.Databases(strDataBase, "dbo")

' Script User Defined Data Types
For Each genObj In db.UserDefinedDatatypes
genObj.Script intOptions, StrFilePath
Next

' Script Tables and Triggers, ignoring system
' tables and system generated triggers
For Each genObj In db.Tables
If genObj.SystemObject = False Then
genObj.Script intOptions, StrFilePath,,SQLDMOScript2_NoCollation
For Each objTrigger In genObj.Triggers
If objTrigger.SystemObject = False Then
objTrigger.Script intOptions, StrFilePath
End If
Next
End If
Next

' Script Rules
For Each genObj In db.Rules
genObj.Script intOptions, StrFilePath
Next

' Script Defaults
For Each genObj In db.Defaults
genObj.Script intOptions, StrFilePath
Next

' Script Sprocs, ignoring system sprocs
For Each genObj In db.StoredProcedures
If genObj.SystemObject = False Then
genObj.Script intOptions, StrFilePath
End If
Next

' Script Views, ignoring system views and informational schemas
For Each genObj In db.Views
If genObj.SystemObject = False Then
genObj.Script intOptions, StrFilePath
End If
Next


End Sub

To call the procedure save the module and supply the parameters.
Call ScriptDB("UserName","Password","DatabaseName","C:\DBScript.SQL")

1 comment:

aiya said...
This comment has been removed by a blog administrator.