Does anyone know where on the system tables i can find information as to whether a table has a primary key or not? I've tried using sysobects and sysindexes (keycnt), but this doesn't appear to be correct. I'm attempting to add primary keys to existing tables and want to check whether they exist before adding them to the neccessary tables.
The code i have is:
IF NOT EXISTS(SELECT * FROM dbo.sysobjects a JOIN dbo.sysindexes b ON a.id=b.id WHERE a.id = object_id(@DataName) AND a.name = @DataName AND b.keycnt > 0)
BEGIN
Do what I have to do
END
The variable @DataName is passed to the code by a cursor, but I have tables with no primary keys that don't get them added. Any help would be much appreciated,
Matt
The code i have is:
IF NOT EXISTS(SELECT * FROM dbo.sysobjects a JOIN dbo.sysindexes b ON a.id=b.id WHERE a.id = object_id(@DataName) AND a.name = @DataName AND b.keycnt > 0)
BEGIN
Do what I have to do
END
The variable @DataName is passed to the code by a cursor, but I have tables with no primary keys that don't get them added. Any help would be much appreciated,
Matt