Scripting Stored Procedures/User Defined Functions
wil
Posts: 7
I am trying to automate scripting a SQL Server 2005 database. I have most of the steps covered, but the one I thought would be easy is giving me some trouble.
Currently, we use the Generate Scripts task from SQL Management Studio. We select all stored procedures and user-defined functions and it scripts out great.
When I do this using SQL Compare the script doesn't work. The procs scripted first reference later procs/functions that haven't been added yet and it all blows up. Can anyone help me out?
-Thanks
Currently, we use the Generate Scripts task from SQL Management Studio. We select all stored procedures and user-defined functions and it scripts out great.
When I do this using SQL Compare the script doesn't work. The procs scripted first reference later procs/functions that haven't been added yet and it all blows up. Can anyone help me out?
-Thanks
Comments
If you want a script of an entire database that you can run straight into a database with Management Studio, in one file, the easiest way to do it is as follows:
1) Create an empty database
2) Compare the database you want to script to the empty database
3) (Optional) Select the 'Do not use transactions in synchronization scripts' option for cleaner-looking output
4) Go through the synchronization wizard until you get the 'View SQL Script...' button
5) Save the resulting SQL script to a file
This should get everything scripted out in the right order for running into an empty database.
Redgate Software
1) Create an empty database on some database server you have access to
2) Create and register Database objects for that empty database and the database you want to script
3) Get a Differences object by comparing the databases with CompareWith
4) Make a Work object and BuildFromDifferences from the Differences object (using the option Options.NoSqlPlumbing if you want cleaner-looking output with no transactions)
5) Get the ExecutionBlock from the Work object and save the contents to a file with SaveToFile
I can provide a code example if you need it, but if you're familiar with the toolkit hopefully the above overview is enough to get you going.
Redgate Software
Is there something else I have to do to only get stored procedure/user-defined function changes?
You'll need to specify each option that you want individually, instead of using Options.Default, if you want to avoid including table changes (to tables that the stored procedures and functions refer to). Note that this does mean that if your new stored procedures and functions really do depend on one of those changes, your script will blow up.
Redgate Software
However, I have 2 other problems I am seeing.
1. If a stored procedure has been renamed, the script fails. Example, sp_OldProc is renamed to sp_NewProc. Scripting this object out using SQL Management Studio, creates a working script. Using red-gate I get something like this:
PRINT 'Scripting object sp_NewProc'
CREATE PROC sp_OldProc .....
Is there any way to get around this?
2. Some of our procs contain special characters like, • »«, etc.
These get scripted out as ?
Anyway to avoid this behavior and have it script out correctly? This also works fine from management studio (we usually select ANSI encoding)
2) SaveToFile has a variety of encoding options - if you use ASCII encoding you'll get ?s instead of special characters. If you're still getting this problem with Unicode encoding, it's probably a bug and we'll look into it.
http://help.red-gate.com/help/SQLCompar ... gType.html
Redgate Software
How do I get a newer version?
To download the new release, please use the check for updates function in your existing installation's help menu, or visit the product page.
Regards,
Jonathan
-Project Manager
-Red Gate Software Ltd
We don't have an option to output ANSI - we only script to ASCII, UTF-8 or Unicode. (Outputting ANSI is slightly awkward, because you need to be sure you're working with the right 'code page' - which affects what that eighth bit means - and this changes between countries. As ANSI is an old standard and UTF-8 works much better in almost all circumstances, we decided not to try and support it.)
If you want your scripts to be similar to the ANSI versions, I recommend UTF-8 - Unicode will make all your files twice the size they were with ANSI encoding, whereas UTF-8 should be about the same.
Redgate Software