Hi,
I am creating a tool for generating SQL Scripts.
I want the sql statement for getting the table name , column name ,
reference table name ,reference column name of a particular foreign key.
In this case I know only the foreign key name.
Thanks and Regards,
SathiamoorthyOJ has written 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
"Sathiamoorthy" <someone@.microsoft.com> wrote in message
news:OZP0tPJLGHA.536@.TK2MSFTNGP09.phx.gbl...
> Hi,
> I am creating a tool for generating SQL Scripts.
> I want the sql statement for getting the table name , column name ,
> reference table name ,reference column name of a particular foreign key.
> In this case I know only the foreign key name.
> Thanks and Regards,
> Sathiamoorthy
>|||Thank you very much.
sathyamoorthy
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:uGo7LUJLGHA.1180@.TK2MSFTNGP09.phx.gbl...
> OJ has written 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
> "Sathiamoorthy" <someone@.microsoft.com> wrote in message
> news:OZP0tPJLGHA.536@.TK2MSFTNGP09.phx.gbl...
>
Showing posts with label generating. Show all posts
Showing posts with label generating. Show all posts
Subscribe to:
Posts (Atom)