Checking if a culomn is a primary key with System Tables

This will return 1 if the column specified is a primary key. Works with SQL Server 2000.

You just have to replace MY_TABLE_NAME and CULOMN_NAME in whe query


select
count(*)
from sysindexes ind,
sysindexkeys indkey,
sysobjects obj,
sysobjects obj2,
syscolumns col
where indkey.id + indkey.indid = ind.id + ind.indid and
ind.name = obj.name and
ind.id = obj.parent_obj and
col.id = indkey.id and
obj.parent_obj = col.id and
obj.parent_obj = obj2.id and
col.colid = indkey.colid and
obj.xtype = 'PK' and
obj2.name = 'MY_TABLE_NAME' and
col.name = 'CULOMN_NAME'

No comments: