Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

How to Get the Scripts for SQL Server Objects Expand / Collapse
Author
Message
Posted Wednesday, March 25, 2009 8:17 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, August 21, 2013 8:04 AM
Points: 56, Visits: 133
Hi,

The idea behind this script is to use it for automation purposes. There are different ways in which it can be achieved. This is just one method.

thanks for the feedback


http://ozkary.blogspot.com
http://og-bit.com
Post #683324
Posted Wednesday, March 25, 2009 11:15 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, March 18, 2011 4:24 PM
Points: 1, Visits: 12
Would it be possible to include sample output for this? I'm particularly interested to see what you get for stored procedures. Is it possible to recover the SQL that was used to create the procedures if you have admin rights to the db?
Post #683500
Posted Wednesday, March 25, 2009 12:18 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 10, 2013 10:42 AM
Points: 3, Visits: 30
At the risk of sounding lazy, (it's more that I am neither a DBA nor a vb coder - I just do some becuase of my job) would it be possible to get a complete code? I am sort of at a loss as to how to combine this all into one "final" program...
Post #683560
Posted Wednesday, March 25, 2009 12:20 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, April 04, 2014 1:13 PM
Points: 384, Visits: 376
Will this script out system objects? In MSSQL 2000 I could "Create Object To New Window As Create" even on system objects (in the Master database). I can't figure out how to do that in MSSQL 2005. I need to see how some system views and functions work.
Post #683564
Posted Wednesday, March 25, 2009 2:52 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, April 04, 2014 1:13 PM
Points: 384, Visits: 376
Well, I finally found out how to Script the system objects.

http://weblogs.sqlteam.com/mladenp/archive/2007/03/12/60132.aspx

Shut down your database, go to the directory with the database files, copy mssqlsystemresource.mdf and mssqlsystemresource.ldf to new names, like mssqlsystemresource1.mdf and mssqlsystemresource1.ldf. Start up the database and attach your new database. Then you can Script the system stuff.


Post #683700
Posted Wednesday, March 25, 2009 3:03 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, June 15, 2012 2:49 PM
Points: 10, Visits: 61
Why didn't they bother to include a copy of a working version of the script? I have yet to see a script on this site that works as advertised out of the box.

On Windows Server 2003, SQL2005, I had to put in a call to Main to get Main to run, replace StdOut with WScript.out to get it to list out the objects to the screen. However the subfolders do not get created and the files don't get written, even if I create the subfolders. The GetFolders function never gets called.

Here's what I've got so far. Anybody want to take a crack at finishing it?


'The usage for a SQL 2000/2005/2008 server with SQL Server authentication is:
'cscript 0g_sqlextract.vbs localhost mydatabase c:\sqloutput sa mypassword
'cscript scriptobjs.vbs localhost data_fusion c:\sql_obj_output df_runtime sheckels
'This script connects to a SQL Server database using Windows or SQL Server authentication. Once it is connected, it iterates through the collection of objects in the database and scripts each one of them

'The command line syntax to run the script is as follows:

'cscript sqlextract.vbs [server] [database] [output folder] [username] [password]

' * [server] server location/ip address/instance name
' * [database] database name/initial catalog
' * [output folder] a folder (must exists) where to write the files too
' * [username] user (optional - sql authentication)
' * [password] password (optional)

'The usage for a SQL Express instance with Windows authentication is:
'cscript 0g_sqlextract.vbs localhost\my-sql-instance mydatabase c:\sqloutput


const SQLDMODep_Children = 262144 'List all Microsoft® SQL Server components that depend on the referenced SQL Server component.
const SQLDMOScript_IncludeHeaders = 131072 'Apply descending order to returned list.
const SQLDMOScript_DRI_AllConstraints=520093696 'SQLDMOScript_DRI_Checks, SQLDMOScript_DRI_Defaults, SQLDMOScript_DRI_ForeignKeys, SQLDMOScript_DRI_PrimaryKey, and SQLDMOScript_DRI_UniqueKeys
const SQLDMOScript_ToFileOnly = 64 'output file
const SQLDMOScript_OwnerQualify = 262144 'object owner
const SQLDMOScript_PrimaryObject = 4 'Generate Transact-SQL creating the referenced component.
const SQLDMOScript_ObjectPermissions = 2 'Include Transact-SQL privilege defining statements when scripting database objects.
const SQLDMOScript_IncludeIfNotExists =4096 'if exists
const SQLDMOScript_Indexes = 73736 'indexs
const SQLDMOScript2_NoCollation = 8388608 'no collation
const SQLDMOScript_Triggers = 16 'triggers
const SQLDMOScript_Drops = 1 'Generate Transact-SQL to remove the referenced component. Script tests for existence prior attempt to remove component.

call Main()
sub Main

'get the parameter list
dim objArgs: Set objArgs = WScript.Arguments

if objArgs.Count > 2 then
connString = objArgs(0) 'connection
database = objArgs(1) 'database
folder = objArgs(2) 'output folder

dim user
dim pw

Set oSql = WScript.CreateObject("SQLDMO.SQLServer2")

if objArgs.Count > 4 then
user = objArgs(3)
pw = objArgs(4)
Wscript.Echo "SQL Authentication - Connection to database"
oSql.LoginSecure = False 'sql authentication
oSql.Connect connString ,user,pw

else

Wscript.Echo "windows Authentication - Connection to database"
oSql.LoginSecure = True 'windows authentication
oSql.Connect connString

end if

call ScriptObjects(oSQL.Databases(database).Tables,"TAB","Reading tables: ")
call ScriptObjects(oSQL.Databases(database).Views,"VIW","Reading views: ")
call ScriptObjects(oSQL.Databases(database).StoredProcedures,"PRC","Reading procedures: ")
call ScriptObjects(oSQL.Databases(database).UserDefinedFunctions,"UDF","Reading functions: ")

oSql.Disconnect

else
Wscript.Echo "Usage: sqlextract.vbs [server or sql instance name] [database] [output folder]"
end if

set oSql = nothing

if err.Description <> "" then
Wscript.Echo err.Description
end if

end sub

sub ScriptObjects(list,ext,msg)

Dim object
Wscript.Echo ""
Wscript.Echo msg

'set the scripting options
dim options
options = SQLDMOScript_Indexes _
OR SQLDMOScript_Drops _
OR SQLDMOScript_IncludeIfNotExists _
OR SQLDMOScript_OwnerQualify _
OR SQLDMOScript_PrimaryObject _
Or SQLDMODep_Children _
Or SQLDMOScript_DRI_AllConstraints _
Or SQLDMODep_IncludeHeaders _
Or SQLDMOScript_ObjectPermissions _
Or SQLDMOScript_ToFileOnly _
OR SQLDMOScript2_NoCollation _
OR SQLDMOScript_Triggers

For Each object In list

If Not object.SystemObject Then
Wscript.Echo object.Name
' Something like this?? call GetFolderName(ext)
' What does the Script function do?
object.Script options, folder + "\" + object.Owner + "." + object.Name + "." + ext
End If

Next

end sub

function GetFolderName(ext)

dim tmpFolder
tmpFolder = ""
select case ext
case "TAB" tmpFolder = TAB_FOLDER
case "VIW" tmpFolder = VIW_FOLDER
case "PRC" tmpFolder = PROC_FOLDER
case "UDF" tmpFolder = UDF_FOLDER
end select

' I added the "call" and parens
call CreateFolder (folder + tmpFolder)
GetFolderName = folder + tmpFolder

end function

sub CreateFolder(path)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

if not fso.FolderExists(path) then
fso.CreateFolder(path)
end if

set fso=nothing

end sub


Post #683709
Posted Wednesday, March 25, 2009 3:07 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, April 04, 2014 1:13 PM
Points: 384, Visits: 376
*Sigh* Except the scripted results may not be usable. For example, I scripted sys.server_principals and the resulting Create script shows it selecting from master.sys.sysxlgns which I can't find anywhere and you can't select from it.
Post #683714
Posted Wednesday, March 25, 2009 4:01 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, June 15, 2012 2:49 PM
Points: 10, Visits: 61
Why would you need to recreate system objects though?
Post #683737
Posted Wednesday, March 25, 2009 4:39 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:26 PM
Points: 6,546, Visits: 8,770
ssaari (3/25/2009)
Why didn't they bother to include a copy of a working version of the script? I have yet to see a script on this site that works as advertised out of the box.

On Windows Server 2003, SQL2005, I had to put in a call to Main to get Main to run, replace StdOut with WScript.out to get it to list out the objects to the screen. However the subfolders do not get created and the files don't get written, even if I create the subfolders. The GetFolders function never gets called.

Here's what I've got so far. Anybody want to take a crack at finishing it?



I took what you have, and refined it. This works. But in looking at the resultant scripts (especially for the tables), I see issues.


'The usage for a SQL 2000/2005/2008 server with SQL Server authentication is:
'cscript 0g_sqlextract.vbs localhost mydatabase c:\sqloutput sa mypassword
'cscript scriptobjs.vbs localhost data_fusion c:\sql_obj_output df_runtime sheckels
'This script connects to a SQL Server database using Windows or SQL Server authentication. Once it is connected, it iterates through the collection of objects in the database and scripts each one of them

'The command line syntax to run the script is as follows:

'cscript sqlextract.vbs [server] [database] [output folder] [username] [password]

' * [server] server location/ip address/instance name
' * [database] database name/initial catalog
' * [output folder] a folder (must exists) where to write the files too
' * [username] user (optional - sql authentication)
' * [password] password (optional)

'The usage for a SQL Express instance with Windows authentication is:
'cscript 0g_sqlextract.vbs localhost\my-sql-instance mydatabase c:\sqloutput


const SQLDMODep_Children = 262144 'List all Microsoft® SQL Server components that depend on the referenced SQL Server component.
const SQLDMOScript_IncludeHeaders = 131072 'Apply descending order to returned list.
const SQLDMOScript_DRI_AllConstraints=520093696 'SQLDMOScript_DRI_Checks, SQLDMOScript_DRI_Defaults, SQLDMOScript_DRI_ForeignKeys, SQLDMOScript_DRI_PrimaryKey, and SQLDMOScript_DRI_UniqueKeys
const SQLDMOScript_ToFileOnly = 64 'output file
const SQLDMOScript_OwnerQualify = 262144 'object owner
const SQLDMOScript_PrimaryObject = 4 'Generate Transact-SQL creating the referenced component.
const SQLDMOScript_ObjectPermissions = 2 'Include Transact-SQL privilege defining statements when scripting database objects.
const SQLDMOScript_IncludeIfNotExists =4096 'if exists
const SQLDMOScript_Indexes = 73736 'indexs
const SQLDMOScript2_NoCollation = 8388608 'no collation
const SQLDMOScript_Triggers = 16 'triggers
const SQLDMOScript_Drops = 1 'Generate Transact-SQL to remove the referenced component. Script tests for existence prior attempt to remove component.
const TAB_FOLDER = "Tables"
const VIW_FOLDER = "Views"
const PROC_FOLDER = "Procedures"
const UDF_FOLDER = "UDFs"

call Main()

'******************************************************************************
sub Main
'******************************************************************************

'get the parameter list
dim objArgs: Set objArgs = WScript.Arguments

if objArgs.Count > 2 then
connString = objArgs(0) 'connection
database = objArgs(1) 'database
folder = objArgs(2) 'output folder
if left(folder,1) <> "\" then folder = folder & "\"

dim user
dim pw

Set oSql = WScript.CreateObject("SQLDMO.SQLServer2")

if objArgs.Count > 4 then
user = objArgs(3)
pw = objArgs(4)
Wscript.Echo "SQL Authentication - Connection to database"
oSql.LoginSecure = False 'sql authentication
oSql.Connect connString ,user,pw
else
Wscript.Echo "windows Authentication - Connection to database"
oSql.LoginSecure = True 'windows authentication
oSql.Connect connString
end if

call ScriptObjects(oSQL.Databases(database).Tables,"TAB","Reading tables: ", folder)
call ScriptObjects(oSQL.Databases(database).Views,"VIW","Reading views: ", folder)
call ScriptObjects(oSQL.Databases(database).StoredProcedures,"PRC","Reading procedures: ", folder)
call ScriptObjects(oSQL.Databases(database).UserDefinedFunctions,"UDF","Reading functions: ", folder)

oSql.Disconnect
else
Wscript.Echo "Usage: sqlextract.vbs [server or sql instance name] [database] [output folder]"
end if

set oSql = nothing

if err.Description <> "" then
Wscript.Echo err.Description
end if

end sub




'******************************************************************************
sub ScriptObjects(list,ext,msg,folder)
'******************************************************************************
Dim object
Wscript.Echo ""
Wscript.Echo msg

'set the scripting options
dim options
options = SQLDMOScript_Indexes _
OR SQLDMOScript_Drops _
OR SQLDMOScript_IncludeIfNotExists _
OR SQLDMOScript_OwnerQualify _
OR SQLDMOScript_PrimaryObject _
Or SQLDMODep_Children _
Or SQLDMOScript_DRI_AllConstraints _
Or SQLDMODep_IncludeHeaders _
Or SQLDMOScript_ObjectPermissions _
Or SQLDMOScript_ToFileOnly _
OR SQLDMOScript2_NoCollation _
OR SQLDMOScript_Triggers

For Each object In list
If Not object.SystemObject Then
Wscript.Echo object.Name
' Something like this??
MyFolder = GetFolderName(ext, folder)
' What does the Script function do?
object.Script options, MyFolder + "\" + object.Owner + "." + object.Name + "." + ext
End If
Next

end sub

'******************************************************************************
function GetFolderName(ext, folder)
'******************************************************************************
dim tmpFolder
tmpFolder = ""
select case ext
case "TAB" tmpFolder = TAB_FOLDER
case "VIW" tmpFolder = VIW_FOLDER
case "PRC" tmpFolder = PROC_FOLDER
case "UDF" tmpFolder = UDF_FOLDER
end select

'WScript.Echo folder + tmpFolder

' I added the "call" and parens
call CreateFolder (folder + tmpFolder)
GetFolderName = folder + tmpFolder

end function

'******************************************************************************
sub CreateFolder(path)
'******************************************************************************
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

if not fso.FolderExists(path) then
fso.CreateFolder(path)
end if

set fso=nothing

end sub


Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #683751
Posted Wednesday, March 25, 2009 7:01 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, June 15, 2012 2:49 PM
Points: 10, Visits: 61
Thank you, Wayne,

I looked at a couple of table scripts and didn't notice anything wrong. What kind of issues did you see if you don't mind?

Scott
Post #683792
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse