The following code sample uses the
Script method of SQL-DMO to generate a Transact-SQL command batch. You can use this command batch to re-create the following objects from any user database:
- Tables
- Views
- Stored Procedures
- Triggers
- User-Defined Data Types
- Rules
- Defaults
Note that the demonstration code generates Transact-SQL to re-create SQL Server objects only.
NOTE: The code does not save or restore user data. If you need to back up data in your SQL Server or MSDE database, use the
Backup command. To do this, in an Access project, click the
Tools menu, point to
Database Utilities, and then click
Backup.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
To generate an SQL Script, follow these steps:
- Open Microsoft Access 2000, and create a new, blank database. Name it SQLScript.
NOTE: The following code will work as written when run from an Access 2000 project that is on the same computer as the SQL Server. To run this code from an Access project that will generate a script for a remote SQL Server database, you must modify the following line:
sql.Connect "(local)", strLogin, strPwd
Change "(local)" to reflect the name of your SQL Server.
Also note that this code will only work when run from a computer with Access 2000 and either MSDE or the SQL Server Client Tools installed.
- Create a new module and type the following line in the Declarations section if it is not already there:
- On the Tools menu, click References. In the list of available references, click to select Microsoft SQLDMO Object Library. Click OK.
- Type the following procedure:
Sub ScriptDB(strLogin As String, strPwd As String, _
strDataBase As String, StrFilePath As String)
'==========================================================================
' Parameters
' -------------------------------------------------------------------------
' strLogin: The Login Name of the account you use to connect to the server
' strPwd: The Password for the account you use to connect to the server
' strDataBase: The name of the database you want to create script for
' StrFilePath: The path and filename to store the SQL file
'==========================================================================
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
' Set scripting options. Because you 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
' Connect to local 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
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
MsgBox "Finished generating SQL scripts."
End Sub
- Save the module as MyModule.
- To call the procedure, open the Immediate window, type the following line, and then press ENTER:
Call ScriptDB("UserName","Password","DatabaseName","C:\MyResults.SQL")
NOTE: In this sample, replace UserName and Password with the logon account and password of a user who has permissions on the server. Replace DatabaseName with the name of the database in which you want to script objects. Replace C:\MyResults.SQL with the path and name of the file that you want the code to generate.