Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
degraft
Starting Member
10 Posts |
Posted - 2009-08-17 : 10:16:09
|
| Hi All,I wrote a script to select all user defined tables in a database:SELECT NAME FROM sys.objects WHERE Type = 'U'.But I would now want to select all the other tables that are related to a particular table based on the primary key-foreign key relationship. i.e if I select a table, I should be able to view the tables that are related to it.Thanks very much in advance.Regards,Degraft.DD |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-17 : 10:46:34
|
| One simple way is to use: exec sp_help <table_name>it returns multiple result sets with a lot of info about the table.Be One with the OptimizerTG |
 |
|
|
degraft
Starting Member
10 Posts |
Posted - 2009-08-17 : 11:08:23
|
| Hi TG,Thanks so much for the help.I've executed that with a test database and it gave me a lot of information about the table including the Tables referenced by foreign key. But what should I do to get ONLY the names of the Tables referenced by the foreign key because I really dont need the other information.Thanks.Degraft. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-17 : 11:11:45
|
| One thing you could do is look at the code of sp_help (from master system SPs) and create your own script with using just the portion you want. It is a good way to learn something about the system tables as well.Be One with the OptimizerTG |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2009-08-17 : 11:32:27
|
or can just look in sys.foreign_keys tableDeclare @tbl sysnameSet @tbl = 'YourTable'SELECT object_name(referenced_object_id), 'refers to' from sys.foreign_keys where parent_object_id = object_id(@tbl)union allSELECT object_name(parent_object_id), 'referenced by' from sys.foreign_keys where referenced_object_id = object_id(@tbl) |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-17 : 11:39:21
|
Or this:select table_name = object_name(fc.parent_object_id) ,column_name = pc.name ,referenced_table = object_name(fc.referenced_object_id) ,referenced_column = rc.name from sys.foreign_key_columns fcjoin sys.syscolumns rc on rc.id = fc.referenced_object_id and rc.colid = fc.referenced_column_idjoin sys.syscolumns pc on pc.id = fc.parent_object_id and pc.colid = fc.parent_column_idwhere parent_object_id = object_id('<yourTable>')Be One with the OptimizerTG |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-08-18 : 00:03:31
|
| Hi Try this once,select * from sys.objects where type = 'pk'select * from sys.objects where type = 'f' |
 |
|
|
degraft
Starting Member
10 Posts |
Posted - 2009-08-18 : 05:50:24
|
| Hi All,Thank you very much for the assistance.TG, thanks so much for the information on the master system SPs. I will make time and get into details with it. Well the script you gave me run but brought only one record:select table_name = object_name(fc.parent_object_id) ,column_name = pc.name ,referenced_table = object_name(fc.referenced_object_id) ,referenced_column = rc.name from sys.foreign_key_columns fcjoin sys.syscolumns rc on rc.id = fc.referenced_object_id and rc.colid = fc.referenced_column_idjoin sys.syscolumns pc on pc.id = fc.parent_object_id and pc.colid = fc.parent_column_idwhere parent_object_id = object_id('tbCommunities')Result:tbCommunities DistrictID tbDistricts DistrictIDRussel, your script did exactly what i wanted only that the first record brought a table that my primary table is linked to. I woudn't want that one.Declare @tbl sysnameSet @tbl = 'tbCommunities'SELECT object_name(referenced_object_id), 'refers to' from sys.foreign_keys where parent_object_id = object_id(@tbl)union allSELECT object_name(parent_object_id), 'referenced by' from sys.foreign_keys where referenced_object_id = object_id(@tbl)Result:tbDistricts refers totbEducation referenced bytbPFIs referenced bytbPipeSource referenced bytbPointWaterSource referenced bytbPreCoolers referenced bytbMarkets referenced bytbStandPipes referenced byI actually found another script that did just what i needed and would like to share it too:selecttblAll.table_name as PrimaryTableName,tblAll.column_name as PrimaryTableColumn,tblFK.table_name as ForeignKeyTable,tblFK.column_name as ForeignKeyColumnfrom information_schema.constraint_column_usage tblAllinner join information_schema.referential_constraints tblAllFK on tblAllFK.unique_constraint_name = tblAll.constraint_nameinner join information_schema.constraint_column_usage tblFK on tblAllFK.constraint_name=tblFK.constraint_nameWhere tblAll.table_name='tbCommunities'tbCommunities CommID tbEducation CommIDtbCommunities CommID tbMarkets CommIDtbCommunities CommID tbPFIs CommIDtbCommunities CommID tbPipeSource CommIDtbCommunities CommID tbPointWaterSource CommIDtbCommunities CommID tbPreCoolers CommIDtbCommunities CommID tbStandPipes CommIDHey Thanks so much you guy. I appreciate it.DD |
 |
|
|
|
|
|
|
|