Obtener Llave primaria de una tabla usando tablas del sistema.
Script de procedimiento almacenad0 que permite ingresar el nombre de una tabla y devuelve su respectiva llave primaria, consultando las tablas del sistema sysobjects, sysindexkeys y syscolumns.
Create procedure ObtenerLlave
@table_name nvarchar(150)
as
SELECT [name]
FROM syscolumns
WHERE [id] IN (
SELECT [id] FROM sysobjects
WHERE [name] = @table_name )
AND colid IN (
SELECT sysindexkeys.colid
FROM sysindexkeys JOIN sysobjects ON sysindexkeys.[id] = sysobjects.[id]
WHERE sysindexkeys.indid = 1 AND sysobjects.[name] = @table_name )