Andras

Software Architect - Red Gate Software

How to order creation SQL files in SQL Compare 6.0 Professional

Published Monday, May 21, 2007 10:00 AM

It is a good practice to store creation scripts for the database organized in a way that there is a single SQL file for each database object. The problem starts when one wants a single script that creates all of these database objects.

I’ve seen a solution in which the creation SQL files are concatenated, and then executed. Obviously the first time some dependencies were not right, so the script failed. This can occur for many reasons, e.g. a foreign key creation is executed before the referenced table is created. So the next step is to look at the error message, and reorder the problematic object. One executes this script, and looks at the error message again. Finally there will be a script that executes without a problem. Seemingly. One can create a stored procedure that references another, before the referenced stored procedure is created. So for example, if the stored procedure ProcA is calling ProcB, one can create ProcA before ProcB is created. Assuming that ProcB is created afterwards, there is no difference in terms of the way ProcA is executed. However, the sysdepends table will not be updated properly.

 

SQL Compare 6.0 Professional will provide a solution to the above task. It can read in creation SQL files from a folder (the final version will read in recursively), and compare the scripts against a live database. If you compare these scripts against an empty live database, you can use the synchronize wizard to create a script that orders the object in the correct dependency order.

There are many options that can influence how this ueber script is assembled. First, you can use “Do not use transactions in synchronization SQL scripts”. If this option is set, SQL Compare will basically just concatenate the creation SQL files in the right dependency order. If this option is not set, then transactions are added to ensure that the whole script is executed as a single transaction.

 

Another useful way to influence the generated script is to compare the creation script files against a specific version of SQL Server. If the empty database is on SQL Server 2005, then the generated script will also use SQL Server 2005 syntax when it is available.

 

SQL Compare 6 processes the scripts that it reads in, as it is seen in the above example, when SQL Compare can decide on the syntax to use. If one wants to just see a list of database objects in the order they should be executed, one can look at the action plan SQL Compare generates, and order the scripts manually, but with a bit of “help”.

 

While ordering SQL creation scripts is not the primary use of SQL Compare, it is a powerful means to create a single SQL creation script from a large number of SQL files.

by András

Comments

 

Randy In Marin said:

Why not order by object type (e.g., schemas, users, db and app roles, full text catalogs, user defined types, xml schemas, udf, tables, PK, IX (tables), FK, constraints, views, IX (views), procs, triggers, db triggers, and synonyms)?  I think delayed name resolution will permit items to be created (e.g., a fn referencing a fn, proc, or table still to be created).  The script should run - perhaps with sysdepends warnings.  The sysdepends data might be off, but should it be trusted anyway?  This would be a good reason to use separate files for each type of object.  
May 31, 2007 11:32 AM
 

András said:

Hi Randy,
I wish we could do what you suggest, however delayed resolution does not work with SQL Server. Say you want to alter a table and add a column. If stored procedure references this table and the new column, altering the stored procedure or even creating the stored procedure will fail, since SQL Server can resolve the table reference, and will check for the existence of the referenced columns.
Another example is a computed column that references a function. If the function does not exist, the table cannot be created. Also many people are using schema binding, so creating the functions first would not work.
Regards,
  Andras
June 7, 2007 3:05 AM
 

Randy In Marin said:

Hi Andras,
I believe you are correct in that there is no delayed name resolution for columns, so it would be a problem unless all the scripts are create scripts.  A single script that creates the database is one thing - which is what I assumed you where talking about.  Migration scripts for an update are another matter.  I've been reading your blog because I need guidance on this topic.  
I was thinking that storing the alter scripts along with the original table definition might be what we end up doing.  However, it appears that you are saying we should use the required table create script and use a "smart" compare tool to create the migration script that has the pieces in the correct order.  (I hope it does not rely upon sysdepends.)
Do you have thoughts on how to handle data for new columns?  Metadata queries for each migration, included in source control, that the tool uses if a column does not exist?  Otherwise, it's difficult to just compare and migrate without a complete history of all data modifications related to migrations.  
Thanks,
Randy
June 7, 2007 10:59 AM
You need to sign in to comment on this blog

About András

András Belokosztolszki is the architect of SQL Compare versions 4, 5, 6 and 7, SQL Log Rescue and SQL Refactor. He is focused on database internals, database synchronization and database schema evolution.

















<May 2007>
SuMoTuWeThFrSa
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
Investigating SQL Server 2008 Wait Events with XEVENTS
 Some reasons for the slow-running of database applications aren't obvious. Occasionally, even the... Read more...

Controlling Email Messages using Exchange's Transport Rules
 Some tasks that should have been easy in previous versions of Exchange just weren't. Now, with... Read more...

Software Tool design: The Three Rs
 To understand the full extent of the requirements of your users when you are redesigning a software... Read more...

JSON and other data serialization languages
 The easiest way to speed up an Ajax application is to take out the 'X' and use JSON rather than XML. Of... Read more...

Embedding Help so it will be used
 It is not good enough to make assumptions about the way that users go about getting help when they use... Read more...