November 7, 2005 at 8:33 am
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
WHERE TABLE_NAME = '<<TABLE_NAME>>'
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME = '<<TABLE_NAME>>'
AND
November 7, 2005 at 10:40 am
In case your primary key has more than one column, you would be interested in the column order as well..you can use this:
SELECT
COLS.CONSTRAINT_NAME,COLS.COLUMN_NAME,COLS.ORDINAL_POSITION
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS COLS
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS CONS
ON COLS.CONSTRAINT_NAME = CONS.CONSTRAINT_NAME
WHERE COLS.CONSTRAINT_CATALOG = DB_NAME()
AND COLS.TABLE_NAME = 'XXXX'
AND CONS.CONSTRAINT_TYPE = 'PRIMARY KEY'
ORDER BY COLS.CONSTRAINT_NAME, COLS.ORDINAL_POSITION
You can replace the name of the table with whatever you want and also replace the constraint_type if you want to search for constraints other than the Primary Key.
November 8, 2005 at 2:03 am
Hi!
Below is the query that will give you the primary key constraint names and the columns used in them for all the user created tables in your database.
SELECT OBJ.NAME, COLS.CONSTRAINT_NAME,COLS.COLUMN_NAME,COLS.ORDINAL_POSITION
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS COLS, INFORMATION_SCHEMA.TABLE_CONSTRAINTS CONS,
SYSOBJECTS OBJ
WHERE COLS.CONSTRAINT_NAME = CONS.CONSTRAINT_NAME
AND COLS.TABLE_NAME = OBJ.NAME
AND CONS.CONSTRAINT_TYPE = 'PRIMARY KEY'
AND OBJ.TYPE = 'U'
ORDER BY OBJ.NAME, COLS.CONSTRAINT_NAME, COLS.ORDINAL_POSITION
Indu Jakhar
November 8, 2005 at 3:48 am
From Books online :
Returns primary key information for a single table in the current environment.
sp_pkeys [ @table_name = ] 'name'
[ , [ @table_owner = ] 'owner' ]
[ , [ @table_qualifier = ] 'qualifier' ]
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution 😀
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply