August 3, 2011 at 10:19 am
Is there any query to get the levels of the tables in a database with RI.
for eg : TableA has a foreign key column of TableB, TableB has a foreign key column of TableC.
Table C is Level 0
Table B is Level 1
Table A is Level 2
August 3, 2011 at 10:28 am
Yes there is. Check this one out.
http://jasonbrimhall.info/2011/07/11/foreign-key-hierarchy-update/
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
August 3, 2011 at 10:28 am
As the article explains, it covers both ancestry and hierarchy.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
August 3, 2011 at 11:06 am
Thanks jason for your instant reply.
Query executed successfully, but I am not getting any output. I gave one of my table in your script for "your Table".
August 3, 2011 at 11:39 am
You may want to test with other tables. If there are no FKs related to that table, you should get an empty result set.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
August 3, 2011 at 11:59 am
I tried for many tables, that too join tables which will definitely have foreign keys.
August 3, 2011 at 12:55 pm
Is it an actual foreign key as established on the table or is it only a logical foreign key?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
August 3, 2011 at 2:31 pm
kishoremania (8/3/2011)
I tried for many tables, that too join tables which will definitely have foreign keys.
You might want to check in SSMS under the table properties as well as sys.foreign_keys
SELECT * FROM sys.foreign_keys
WHERE OBJECT_NAME(parent_object_id) = 'your table name'
OR OBJECT_NAME(referenced_object_id) = 'your table name'
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
August 3, 2011 at 2:54 pm
SQLRNNR (8/3/2011)
Yes there is. Check this one out.http://jasonbrimhall.info/2011/07/11/foreign-key-hierarchy-update/
Nice script Jason, I had done something similar for Hierarchy, but mentally skipped over dependencies in the other direction.
i added that to my snippets, thanks!
Lowell
August 3, 2011 at 2:59 pm
Lowell (8/3/2011)
SQLRNNR (8/3/2011)
Yes there is. Check this one out.http://jasonbrimhall.info/2011/07/11/foreign-key-hierarchy-update/
Nice script Jason, I had done something similar for Hierarchy, but mentally skipped over dependencies in the other direction.
i added that to my snippets, thanks!
You are quite welcome. Remi (Ninja) sent me an update to support collations that are CS. I'll be posting the update soon.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
August 4, 2011 at 9:47 am
Thanks for your help. It really helped me. Finally I worked out and got a similar query.
Select
object_name(rkeyid) Parent_Table,
object_name(fkeyid) Child_Table,
object_name(constid) FKey_Name,
c1.name FKey_Col,
c2.name Ref_KeyCol
From
sys.sysforeignkeys s
Inner join sys.syscolumns c1
on ( s.fkeyid = c1.id And s.fkey = c1.colid )
Inner join syscolumns c2
on ( s.rkeyid = c2.id And s.rkey = c2.colid )
Order by Parent_Table,Child_Table
August 4, 2011 at 1:47 pm
kishoremania (8/4/2011)
Thanks for your help. It really helped me. Finally I worked out and got a similar query.Select
object_name(rkeyid) Parent_Table,
object_name(fkeyid) Child_Table,
object_name(constid) FKey_Name,
c1.name FKey_Col,
c2.name Ref_KeyCol
From
sys.sysforeignkeys s
Inner join sys.syscolumns c1
on ( s.fkeyid = c1.id And s.fkey = c1.colid )
Inner join syscolumns c2
on ( s.rkeyid = c2.id And s.rkey = c2.colid )
Order by Parent_Table,Child_Table
Since you are on SQL 2008, I'd recommend updating that query to use the new system objects in lieu of the soon to be deprecated objects.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply