Hi
I'm working with SQL 2000 and I want to know how can I find which tables
have relationships to a particular table.in other words, I need a select
statement to fetch the name of relations and foreign keys that my table
has a relation with them.
Any help would be greatly thankful.OJ wrote this script
create procedure usp_findreferences
@.tbname sysname=null
as
set nocount on
Print 'Referenced:'
select c1.table_name,
c1.column_name,
fkey=r.constraint_name,
referenced_parent_table=c2.table_name,
c2.column_name
from information_schema.constraint_column_usage c1 join
information_schema.referential_constraints r on
c1.constraint_name=r.constraint_name
join information_schema.constraint_column_usage c2 on
r.unique_constraint_name=c2.constraint_name
where c1.table_name=coalesce(@.tbname,c1.table_name)
order by case when @.tbname is null then c1.table_name else c2.table_name end
print ''
print 'Referencing:'
select c1.table_name,
c1.column_name,
fkey=r.constraint_name,
referencing_child_table=c2.table_name,
c2.column_name
from information_schema.constraint_column_usage c1 join
information_schema.referential_constraints r on
c1.constraint_name=r.unique_constraint_name
join information_schema.constraint_column_usage c2 on
r.constraint_name=c2.constraint_name
where c1.table_name=coalesce(@.tbname,c1.table_name)
order by case when @.tbname is null then c1.table_name else c2.table_name end
go
--test run
exec usp_findreferences 'Orders'
drop proc usp_findreferences
<R> wrote in message news:ops8gj1byemw7tkz@.system109.parskhazar.net...
> Hi
> I'm working with SQL 2000 and I want to know how can I find which tables
> have relationships to a particular table.in other words, I need a select
> statement to fetch the name of relations and foreign keys that my table
> has a relation with them.
> Any help would be greatly thankful.
Thursday, February 16, 2012
**find relations of a table**
Labels:
database,
hii,
microsoft,
mysql,
oracle,
particular,
relations,
relationships,
server,
sql,
table,
tableshave,
words,
working
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment