﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by R Glen Cooper  / Ordering Tables To Preserve Referential Integrity / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 18 May 2013 04:31:54 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Ordering Tables To Preserve Referential Integrity</title><link>http://www.sqlservercentral.com/Forums/Topic680152-1514-1.aspx</link><description>Excellent script.  I needed to drop most of the tables in database, in RI order.  This is exactly what I need.Thanks!</description><pubDate>Tue, 16 Nov 2010 07:10:37 GMT</pubDate><dc:creator>Ken Davis</dc:creator></item><item><title>RE: Ordering Tables To Preserve Referential Integrity</title><link>http://www.sqlservercentral.com/Forums/Topic680152-1514-1.aspx</link><description>Thank you for that.Those tables not involved in any relationships are not displayed (by design), but to display them simply add this snippet just before the script displays the final answer:-- Add to answer table those tables involved in no relationships (ie. what's left) -- Use -1 as LevelINSERT INTO tblAnswer([Level], A)SELECT -1, name FROM dbo.sysobjects WHERE OBJECTPROPERTY(id, N'IsUserTable') = 1AND name NOT IN ('sysdiagrams','tblPreorder','tblAnswer')AND name NOT IN(SELECT DISTINCT A FROM tblAnswer)</description><pubDate>Tue, 09 Feb 2010 13:47:50 GMT</pubDate><dc:creator>Glen Cooper</dc:creator></item><item><title>RE: Ordering Tables To Preserve Referential Integrity</title><link>http://www.sqlservercentral.com/Forums/Topic680152-1514-1.aspx</link><description>Very smart, elegant, beautiful solution/idea.Thank u for sharing ur brain power with us, thanks a lot.:-) :w00t:</description><pubDate>Tue, 09 Feb 2010 13:00:08 GMT</pubDate><dc:creator>san_kan1gb</dc:creator></item><item><title>RE: Ordering Tables To Preserve Referential Integrity</title><link>http://www.sqlservercentral.com/Forums/Topic680152-1514-1.aspx</link><description>FYI: I referenced this article on my blog: [url=http://www.sqlservercentral.com/blogs/robert_davis/archive/2009/03/29/Do-You-Support-the-Ancient-Ones.aspx]http://www.sqlservercentral.com/blogs/robert_davis/archive/2009/03/29/Do-You-Support-the-Ancient-Ones.aspx[/url]</description><pubDate>Sun, 29 Mar 2009 11:00:17 GMT</pubDate><dc:creator>Robert Davis</dc:creator></item><item><title>RE: Ordering Tables To Preserve Referential Integrity</title><link>http://www.sqlservercentral.com/Forums/Topic680152-1514-1.aspx</link><description>Wonderful, thank you very much !!!!!I will use it immediatly.Best regards</description><pubDate>Sun, 29 Mar 2009 09:32:25 GMT</pubDate><dc:creator>adrlinux</dc:creator></item><item><title>RE: Ordering Tables To Preserve Referential Integrity</title><link>http://www.sqlservercentral.com/Forums/Topic680152-1514-1.aspx</link><description>OK, this is an ugly kludge for SQL Server 2000.Replace the snippet for creating the preorder table with the one below.I can't think of a better way to do this.Glen-- Create a preorder table using the database -- relationships between tables.  By definition,-- a table A will be 'less than' a table B if -- B has a foreign key pointing to A.-- SQL Server 2000 version. SELECT A, BINTO tblPreorderFROM(SELECT-- May be multiple relationships between two-- tables but we only need oneDISTINCT -- Get name of parent table pointing to foreign tableso2.name as B,-- Get name of foreign table by deleting-- name of parent table (along with punctuation)-- from name of foreign key constraint RIGHT(so1.name,LEN(so1.name) - (LEN(so2.name)+4)) as A FROM sysobjects so1INNER JOINsysobjects so2ONso1.parent_obj = so2.idWHERE-- Restrict sysobjects to foreign key constraints  so1.xtype='F' AND-- Make sure name of foreign table represents -- a real table because sysobjects will append-- 1, 2, etc. to foreign key constraint name if -- multiple relationships exist between a pair of them RIGHT(so1.name,LEN(so1.name) - (LEN(so2.name)+4)) IN (SELECT NAME FROM sysobjects WHERE xtype = 'U')) derivedORDER BY A, B</description><pubDate>Mon, 23 Mar 2009 15:39:22 GMT</pubDate><dc:creator>Glen Cooper</dc:creator></item><item><title>RE: Ordering Tables To Preserve Referential Integrity</title><link>http://www.sqlservercentral.com/Forums/Topic680152-1514-1.aspx</link><description>OK, I'll get back to you on this.Glen</description><pubDate>Mon, 23 Mar 2009 13:50:14 GMT</pubDate><dc:creator>Glen Cooper</dc:creator></item><item><title>RE: Ordering Tables To Preserve Referential Integrity</title><link>http://www.sqlservercentral.com/Forums/Topic680152-1514-1.aspx</link><description>Hello,   Thank you very much for this awesome article ! But would it be possible to have the same script for sql server 2000 ? Badly I am not able to do the transformation.Thank you very much.</description><pubDate>Mon, 23 Mar 2009 12:29:55 GMT</pubDate><dc:creator>adrlinux</dc:creator></item><item><title>RE: Ordering Tables To Preserve Referential Integrity</title><link>http://www.sqlservercentral.com/Forums/Topic680152-1514-1.aspx</link><description>Thanks!! In that case, it seems to work perfectly. :)</description><pubDate>Fri, 20 Mar 2009 12:50:13 GMT</pubDate><dc:creator>Robert Davis</dc:creator></item><item><title>RE: Ordering Tables To Preserve Referential Integrity</title><link>http://www.sqlservercentral.com/Forums/Topic680152-1514-1.aspx</link><description>Yes, tables involved in no relationships are ignored, for simplicity.Glen</description><pubDate>Fri, 20 Mar 2009 12:16:27 GMT</pubDate><dc:creator>Glen Cooper</dc:creator></item><item><title>RE: Ordering Tables To Preserve Referential Integrity</title><link>http://www.sqlservercentral.com/Forums/Topic680152-1514-1.aspx</link><description>I ran the script on a database with three tables:1. dbo.LocationType (no foreign keys2. dbo.Locations (foreign key to dbo.LocationType)3. dbo.Fish (no foreign key defined, but it should have one to dbo.Locations)The script ouput was:Level	A0	LocationType1	LocationsAre tables with no references to or from excluded from the output intentionally? I would have expected dbo.Fish to be returned as a level 0 table.When I added the foreign key to dbo.Fish, I got the expected results of dbo.Fish being a level 2 table.</description><pubDate>Fri, 20 Mar 2009 10:50:46 GMT</pubDate><dc:creator>Robert Davis</dc:creator></item><item><title>RE: Ordering Tables To Preserve Referential Integrity</title><link>http://www.sqlservercentral.com/Forums/Topic680152-1514-1.aspx</link><description>Awesome article and very helpful!!!  I haven't had a chance to go through the code yet, but the description of what you are trying to accomplish is very clear.  I'm excited because I think I might be able to use this code in the future.  If I am making major changes to a database, I will completely re-engineer the database from scratch for both development purposes and later as part of the rollout process.  What I mean by "completely re-engineer..." is that I follow these steps (which happens about 1 or 2 times a year for the major databases):.   a) create new database in SSMS.   b) generate the new database schema from ERwin, the tool I use to store the datamodel and which can be used to add all the tables, indexes, foreign keys etc..   c) run a script that copies all the data from the existing database into the new one with the new schema just created.We have a custom, home-grown application which creates the script for step c).  But that custom application requires that we generate a file from ERwin that will determine the correct order for the tables to do the copying.I've always wondered how we could take that ERwin file out of the picture and figure out the correct table order using SQL.  It is a much cleaner solution for a couple of reasons.  For example, one time when ERwin released an "upgrade", the format of the report/file that we use got changed.  So, we had to change the code in our custom app.  It would be better to not have to rely on ERwin's table generating file.Thanks for the idea!  I hope I will be able to use it in the future at some point.</description><pubDate>Fri, 20 Mar 2009 10:28:08 GMT</pubDate><dc:creator>JJ B</dc:creator></item><item><title>RE: Ordering Tables To Preserve Referential Integrity</title><link>http://www.sqlservercentral.com/Forums/Topic680152-1514-1.aspx</link><description>Could someone fix the table? Its got  &amp;lt;tr&amp;gt's instead of &amp;lt;td&amp;gt's</description><pubDate>Fri, 20 Mar 2009 02:29:16 GMT</pubDate><dc:creator>John Muir</dc:creator></item><item><title>Ordering Tables To Preserve Referential Integrity</title><link>http://www.sqlservercentral.com/Forums/Topic680152-1514-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/articles/Database+Design/66071/"&gt;Ordering Tables To Preserve Referential Integrity&lt;/A&gt;[/B]</description><pubDate>Fri, 20 Mar 2009 00:22:32 GMT</pubDate><dc:creator>Glen Cooper</dc:creator></item></channel></rss>