Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Ordering Tables To Preserve Referential Integrity


Ordering Tables To Preserve Referential Integrity

Author
Message
Robert Davis
Robert Davis
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1972 Visits: 1623
FYI: I referenced this article on my blog: http://www.sqlservercentral.com/blogs/robert_davis/archive/2009/03/29/Do-You-Support-the-Ancient-Ones.aspx



My blog: SQL Soldier
Twitter: @SQLSoldier
My book: Pro SQL Server 2008 Mirroring
Microsoft Certified Master, SQL Server MVP
Database Engineer at BlueMountain Capital Management
san_kan1gb
san_kan1gb
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 33
Very smart, elegant, beautiful solution/idea.
Thank u for sharing ur brain power with us, thanks a lot.
:-) w00t
Glen Cooper
Glen Cooper
SSC-Enthusiastic
SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)SSC-Enthusiastic (127 reputation)

Group: General Forum Members
Points: 127 Visits: 287
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 Level
INSERT INTO tblAnswer([Level], A)
SELECT -1, name FROM dbo.sysobjects
WHERE OBJECTPROPERTY(id, N'IsUserTable') = 1
AND name NOT IN ('sysdiagrams','tblPreorder','tblAnswer')
AND name NOT IN
(
SELECT DISTINCT A FROM tblAnswer
)

R Glen Cooper
Ken Davis
Ken Davis
SSC Veteran
SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)SSC Veteran (219 reputation)

Group: General Forum Members
Points: 219 Visits: 491
Excellent script. I needed to drop most of the tables in database, in RI order. This is exactly what I need.

Thanks!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search