sp_primarykeys (T-SQL)

Returns the primary key columns, one row per key column, for the specified remote table.

Syntax

sp_primarykeys [@table_server =] 'table_server'
    [,[@table_name =] 'table_name']
    [,[@table_schema =] 'table_schema']
    [,[@table_catalog =] 'table_catalog']

Arguments
[@table_server =] 'table_server'
Is the name of the linked server from which to return primary key information. table_server is sysname, with no default.
[@table_name =] 'table_name'
Is the name of the table for which to provide primary key information. table_name is sysname, with a default of NULL.
[@table_schema =] 'table_schema'
Is the table schema. table_schema is sysname, with a default of NULL. In the Microsoft SQL Server environment, this corresponds to the table owner.
[@table_catalog =] 'table_catalog'
Is the name of the catalog in which the specified table_name resides. In the Microsoft SQL Server environment, this corresponds to the database name. table_catalog is sysname, with a default of NULL.
Return Code Values

None

Result Sets
Column name Data type Description
TABLE_CAT sysname Table catalog. 
TABLE_SCHEM sysname Table schema.
TABLE_NAME sysname Name of the table.
COLUMN_NAME sysname Name of the column.
KEY_SEQ int Sequence number of the column in a multicolumn primary key.
PK_NAME sysname Primary key identifier. Returns NULL if not applicable to the data source.

Remarks

If table_name is NULL, sp_primarykeys returns information about all of the tables in the catalog.

sp_primarykeys is executed by querying the PRIMARY_KEYS rowset of the IDBSchemaRowset interface of the OLE DB provider corresponding to table_server. The table_name, table_schema, table_catalog, and column parameters are passed to this interface to restrict the rows returned.

sp_primarykeys returns an empty result set if the OLE DB provider of the specified linked server does not support the PRIMARY_KEYS rowset of the IDBSchemaRowset interface.

Permissions

Execute permissions default to the public role.

Examples

This example returns primary key columns from the LONDON1 server for the Customers table in the Northwind database.

USE master

EXEC sp_primarykeys @table_server = N'LONDON1',
    @table_name = N'Customers',
    @table_catalog = N'Northwind',
    @table_schema = N'dbo'

  

See Also
sp_catalogs sp_linkedservers
sp_column_privileges sp_tables_ex
sp_foreignkeys sp_table_privileges
sp_indexes System Stored Procedures

  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.