Home All Groups Group Topic Archive Search About

PKey association w/table

Author
8 Jul 2005 6:59 PM
Rob
Hi:

Given that the name of a PKey has been identified, how can one tell which
table that PKey is associated to? Any ideas. Thanks.

Author
8 Jul 2005 7:06 PM
Aaron Bertrand [SQL Server MVP]
DECLARE @pkeyname SYSNAME

SET @pkeyname = 'your_primary_key_name'

SELECT
    T.TABLE_NAME,
    T.CONSTRAINT_NAME,
    K.COLUMN_NAME,
    K.ORDINAL_POSITION
FROM
    INFORMATION_SCHEMA.TABLE_CONSTRAINTS T
    INNER JOIN
    INFORMATION_SCHEMA.KEY_COLUMN_USAGE K
    ON T.CONSTRAINT_NAME = K.CONSTRAINT_NAME
WHERE
    T.CONSTRAINT_TYPE = 'PRIMARY KEY'
    AND K.CONSTRAINT_NAME = @pkeyname
ORDER BY
    T.TABLE_NAME,
    K.ORDINAL_POSITION




Show quote
"Rob" <R**@discussions.microsoft.com> wrote in message
news:00E17BBC-52FB-40D1-94C3-03F0E9183FCC@microsoft.com...
> Hi:
>
> Given that the name of a PKey has been identified, how can one tell which
> table that PKey is associated to? Any ideas. Thanks.
Author
8 Jul 2005 7:08 PM
Itzik Ben-Gan
Rob, try:

select table_schema, table_name
from information_schema.table_constraints
where constraint_schema = 'dbo'
  and constraint_name = 'pk_t1'

--
BG, SQL Server MVP
www.SolidQualityLearning.com


Show quote
"Rob" <R**@discussions.microsoft.com> wrote in message
news:00E17BBC-52FB-40D1-94C3-03F0E9183FCC@microsoft.com...
> Hi:
>
> Given that the name of a PKey has been identified, how can one tell which
> table that PKey is associated to? Any ideas. Thanks.
Author
9 Jul 2005 5:11 AM
Louis Davidson
The other answers are valid and good, but, pray tell, where did you identify
this PKey?  A column without the context of a table is just the definition
of a scalar (or vector) value?   One rarely stumbles upon such things :)

--
----------------------------------------------------------------------------
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP


Show quote
"Rob" <R**@discussions.microsoft.com> wrote in message
news:00E17BBC-52FB-40D1-94C3-03F0E9183FCC@microsoft.com...
> Hi:
>
> Given that the name of a PKey has been identified, how can one tell which
> table that PKey is associated to? Any ideas. Thanks.

AddThis Social Bookmark Button