August 18, 2009 at 8:04 pm
Hi,
I am trying to create some automation scripts and need to list the columns of a PK knowing the table name with a TSQL query.
Any ideas?
Thanks.
August 19, 2009 at 2:25 am
Hi,
I think this may be able to help you out;
SELECT SysT.Name AS TableName,
SC.Name AS ColumnName
FROM Sys.Tables SysT
INNER JOIN Sys.SysIndexKeys SysIK ON SysT.object_id = SysIK.id
INNER JOIN sys.syscolumns SC ON SysIK.colid = SC.colid AND SysIK.id = SC.id
WHERE SysT.Name = ''
AND SysIK.indid = 1
Let me know how you get on.
August 19, 2009 at 6:41 am
Another way is to use the information_schema.table_constraints view. Here is an example:
select *
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
where CONSTRAINT_TYPE = 'PRIMARY KEY' and TABLE_NAME = 'WriteTableNameHere'
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply