Technical Article

Get Primary Key columns of given MSSQL table

,

This procedure will give the list of columns included in the primary key for a given table of given database on given server.
For. e.g.
Get_PrimaryKey 'vrs-118','Demo_StDB','IMAGEREPOSITORY'

CREATE PROCEDURE Get_PrimaryKey
@ServerName varchar(50),
@CatalogName varchar(50),
@TableName varchar(50)

AS

EXEC sp_serveroption @ServerName, 'data access', 'true'
select
        COLUMN_NAME = COLUMN_NAME
from master.dbo.SYSREMOTE_PRIMARY_KEYS ( @ServerName,@CatalogName, N'dbo',@TableName )

EXEC sp_serveroption @ServerName, 'data access', 'false'
GO

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating