sp_primarykeys (Transact-SQL)sp_primarykeys (Transact-SQL)

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions)

返回指定远程表的主键列,每个键列对应一行。Returns the primary key columns, one row per key column, for the specified remote table.

主题链接图标 Transact-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

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

参数Arguments

[ @table_server = ] 'table_server'_ 要从中返回主键信息的链接服务器的名称。[ @table_server = ] 'table_server'_ Is the name of the linked server from which to return primary key information. table_server sysname,无默认值。table_server is sysname, with no default.

[ @table_name = ] 'table_name' 要为其提供主键信息的表的名称。[ @table_name = ] 'table_name' Is the name of the table for which to provide primary key information. table_name 的默认值为 sysname,默认值为 NULL。table_name is sysname, with a default of NULL.

[ @table_schema = ] 'table_schema' 表架构。[ @table_schema = ] 'table_schema' Is the table schema. table_schema 的默认值为 sysname,默认值为 NULL。table_schema is sysname, with a default of NULL. SQL ServerSQL Server 环境中,该值对应于表所有者。In the SQL ServerSQL Server environment, this corresponds to the table owner.

[ @table_catalog = ] 'table_catalog' 指定 table_name 所在目录的名称。[ @table_catalog = ] 'table_catalog' Is the name of the catalog in which the specified table_name resides. SQL ServerSQL Server 环境中,该值对应于数据库名称。In the SQL ServerSQL Server environment, this corresponds to the database name. table_catalog 的默认值为 sysname,默认值为 NULL。table_catalog is sysname, with a default of NULL.

返回代码值Return Code Values

None

结果集Result Sets

列名称Column name 数据类型Data type 说明Description
TABLE_CATTABLE_CAT sysnamesysname 表目录。Table catalog.
TABLE_SCHEMTABLE_SCHEM sysnamesysname 表架构。Table schema.
TABLE_NAMETABLE_NAME sysnamesysname 表的名称。Name of the table.
COLUMN_NAMECOLUMN_NAME sysnamesysname 列的名称。Name of the column.
KEY_SEQKEY_SEQ intint 多列主键中列的序列号。Sequence number of the column in a multicolumn primary key.
PK_NAMEPK_NAME sysnamesysname 主键标识符。Primary key identifier. 如果对数据源不适用,则返回 NULL。Returns NULL if not applicable to the data source.

备注Remarks

sp_primarykeys 通过查询与 table_server 对应的 OLE DB 提供程序的 IDBSchemaRowset 接口的 PRIMARY_KEYS 行集来执行。sp_primarykeys is executed by querying the PRIMARY_KEYS rowset of the IDBSchemaRowset interface of the OLE DB provider corresponding to table_server. table_nametable_schematable_catalog 参数传递到此接口,以限制返回的行数。The table_name, table_schema, table_catalog, and column parameters are passed to this interface to restrict the rows returned.

如果指定链接服务器的 OLE DB 提供程序不支持 IDBSchemaRowset 接口的 PRIMARY_KEYS 行集, sp_primarykeys 将返回空结果集。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

需要对架构的 SELECT 权限。Requires SELECT permission on the schema.

示例Examples

以下示例返回 AdventureWorks2012AdventureWorks2012 数据库中 LONDON1 表的 HumanResources.JobCandidate 服务器中的主键列。The following example returns primary key columns from the LONDON1 server for the HumanResources.JobCandidate table in the AdventureWorks2012AdventureWorks2012 database.

EXEC sp_primarykeys @table_server = N'LONDON1',   
   @table_name = N'JobCandidate',  
   @table_catalog = N'AdventureWorks2012',   
   @table_schema = N'HumanResources';  

另请参阅See Also

分布式查询存储过程 (Transact-sql) Distributed Queries Stored Procedures (Transact-SQL)
sp_catalogs (Transact-sql) sp_catalogs (Transact-SQL)
sp_column_privileges (Transact-sql) sp_column_privileges (Transact-SQL)
sp_foreignkeys (Transact-sql) sp_foreignkeys (Transact-SQL)
sp_indexes (Transact-sql) sp_indexes (Transact-SQL)
sp_linkedservers (Transact-SQL) sp_linkedservers (Transact-SQL)
sp_tables_ex (Transact-sql) sp_tables_ex (Transact-SQL)
sp_table_privileges (Transact-sql) sp_table_privileges (Transact-SQL)
系统存储过程 (Transact-SQL)System Stored Procedures (Transact-SQL)