SELECT [name] FROM sysobjects WHERE [xtype] = ‘PK’ AND [parent_obj] = OBJECT_ID(N’[dbo].[' + @tableName + ']‘)
This doesn’t rely on a naming convention of primary keys’ names starting with PK, but instead uses SQL Server’s own data on whether the index is a primary key.
]]>