It all depends on the dependencies. In gross terms, there is a hierarchy: Types, Tables, Views, Functions and Stored Procedures. This generally is the trend but it can get convoluted. The tables can also have dependencies based on foreign keys. As an alternative, you could create the tables but omit the foreign keys under after the data was migrated. There is a system table named "sys.sql_expression_dependencies" that shows which objects rely on others. There is another system table that shows foreign key dependencies. Here are a couple of scripts that show these. They are designed to be used for a single entity but you can probably modify them to be more general. Foreign Keys:-------------------------------------------------------------------------------- cteFKDepends------ Description: Given an object name, find all tables that depend--- on it through a Foreign Key, either directly or indirectly.-----------------------------------------------------------------------------declare @object sysname = N'MyTable' -- <<< SET THIS VALUE-----------------------------------------------------------------------------;with cteDependsas (select Name Root, Name TableName, object_id, cast(Name as varchar(max)) Lineage, 0 Depthfrom sys.tableswhere Name = @Objectunion allselect d.Root, OBJECT_NAME(fk.parent_object_id) TableName, fk.parent_object_id, cast(d.Lineage + '<-' + OBJECT_NAME(fk.parent_object_id) as varchar(max)), Depth + 1from cteDepends dinner join sys.foreign_keys fk on fk.referenced_object_id = d.object_id)select *from cteDependswhere Depth > 0--and root like 'usv_%'order by Root, Depth DESC, Lineage --, TableName
And Objects:-------------------------------------------------------------------------------- cteDepends------ Description: Given an object name, find all schema objects that depend--- on it, either directly or indirectly.-----------------------------------------------------------------------------declare @object sysname = N'MyObject' -- <<< SET THIS VALUE-----------------------------------------------------------------------------;with cteDependsas (select Name Root, Type, Name, cast(Name as varchar(max)) Lineage, 0 Depthfrom sys.objectswhere Name = @Objectunion allselect d.Root, o.Type, OBJECT_NAME(sed.referencing_id) Name, --cast(OBJECT_NAME(sed.referencing_id) + '->' + d.Lineage as varchar(max)), cast(d.Lineage + '<-' + OBJECT_NAME(sed.referencing_id) as varchar(max)), Depth + 1from cteDepends dinner join sys.sql_expression_dependencies sed on sed.referenced_entity_name = d.Name and sed.referencing_class <> 7 -- 7 = Index : 1 = Object/Column (vs Trigger or Index)inner join sys.objects o on o.object_id = sed.referencing_id)select *from cteDepends--where root like 'usv_%'order by Root, Type, Lineage, Name, Depth
Good luck!
No amount of belief makes something a fact. -James Randi