sp_pkeys (Transact-SQL)sp_pkeys (Transact-SQL)
適用対象:Applies to: SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Parallel Data WarehouseParallel Data Warehouse
Parallel Data WarehouseParallel Data Warehouse
SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Parallel Data WarehouseParallel Data Warehouse
Parallel Data WarehouseParallel Data Warehouse
現在の環境における、単一のテーブルに関する主キーの情報を返します。Returns primary key information for a single table in the current environment.
Transact-SQL 構文表記規則
Transact-SQL Syntax Conventions
構文Syntax
-- Syntax for SQL Server, Azure SQL Database, Azure Synapse Analytics, Parallel Data Warehouse
sp_pkeys [ @table_name = ] 'name'
[ , [ @table_owner = ] 'owner' ]
[ , [ @table_qualifier = ] 'qualifier' ]
引数Arguments
[ @table_name =] 'name'[ @table_name= ] 'name'
情報を返すテーブルを指定します。Is the table for which to return information. 名前 は sysname,、既定値はありません。name is sysname, with no default. ワイルドカードのパターンマッチングはサポートされていません。Wildcard pattern matching is not supported.
[ @table_owner =] 'owner'[ @table_owner= ] 'owner'
指定したテーブルのテーブル所有者を指定します。Specifies the table owner of the specified table. owner は sysname,、既定値は NULL です。owner is sysname, with a default of NULL. ワイルドカードのパターンマッチングはサポートされていません。Wildcard pattern matching is not supported. Owner が指定されていない場合、基になる DBMS の既定のテーブル可視性ルールが適用されます。If owner is not specified, the default table visibility rules of the underlying DBMS apply.
では SQL ServerSQL Server 、指定された名前のテーブルが現在のユーザーによって所有されている場合、そのテーブルの列が返されます。In SQL ServerSQL Server, if the current user owns a table with the specified name, the columns of that table are returned. 所有者 が指定されておらず、現在のユーザーが指定された 名前 のテーブルを所有していない場合、このプロシージャは、データベース所有者が所有する、指定された 名前 のテーブルを検索します。If the owner is not specified and the current user does not own a table with the specified name, this procedure looks for a table with the specified name owned by the database owner. そのテーブルが存在する場合、そのテーブルの列が返されます。If one exists, the columns of that table are returned.
[ @table_qualifier =] '修飾子'[ @table_qualifier= ] 'qualifier'
テーブル修飾子を示します。Is the table qualifier. 修飾子 は sysname,、既定値は NULL です。qualifier is sysname, with a default of NULL. さまざまな DBMS 製品では、3つの要素で構成するテーブル (修飾子) がサポート しています。所有者。名前)。Various DBMS products support three-part naming for tables (qualifier.owner.name). では SQL ServerSQL Server 、この列はデータベース名を表します。In SQL ServerSQL Server, this column represents the database name. 一部の製品では、テーブルのデータベース環境のサーバー名を表します。In some products, it represents the server name of the database environment of the table.
リターン コードの値Return Code Values
なしNone
結果セットResult Sets
列名Column name | データ型Data type | 説明Description |
---|---|---|
TABLE_QUALIFIERTABLE_QUALIFIER | sysnamesysname | テーブル修飾子の名前。Name of the table qualifier. このフィールドは NULL にすることができます。This field can be NULL. |
TABLE_OWNERTABLE_OWNER | sysnamesysname | テーブル所有者の名前。Name of the table owner. このフィールドは常に値を返します。This field always returns a value. |
TABLE_NAMETABLE_NAME | sysnamesysname | テーブルの名前。Name of the table. SQL ServerSQL Server では、この列は、sysobjects テーブルに記録されているテーブル名を表します。In SQL ServerSQL Server, this column represents the table name as listed in the sysobjects table. このフィールドは常に値を返します。This field always returns a value. |
COLUMN_NAMECOLUMN_NAME | sysnamesysname | 返される TABLE_NAME の各列の列名。Name of the column, for each column of the TABLE_NAME returned. SQL ServerSQL Server では、この列は、sys.columns テーブルに記録されている列名を表します。In SQL ServerSQL Server, this column represents the column name as listed in the sys.columns table. このフィールドは常に値を返します。This field always returns a value. |
KEY_SEQKEY_SEQ | smallintsmallint | 複数列の主キーの列のシーケンス番号。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_pkeys では、PRIMARY KEY 制約で明示的に定義された列に関する情報が返されます。sp_pkeys returns information about columns explicitly defined with a PRIMARY KEY constraint. すべてのシステムが明示的に名前が付けられた主キーをサポートしているわけではないため、ゲートウェイの実装者が主キーの構成を決定します。Because not all systems support explicitly named primary keys, the gateway implementer determines what constitutes a primary key. "主キー" という用語は、テーブルの論理主キーを指していることに注意してください。Note that the term primary key refers to a logical primary key for a table. 論理主キーとして示されているすべてのキーには、一意のインデックスが定義されている必要があります。It is expected that every key listed as being a logical primary key has a unique index defined on it. sp_statistics には、この一意なインデックスも返されます。This unique index is also returned in sp_statistics.
sp_pkeys ストアド プロシージャは、ODBC の SQLPrimaryKeys に相当します。The sp_pkeys stored procedure is equivalent to SQLPrimaryKeys in ODBC. 返される結果は、TABLE_QUALIFIER、TABLE_OWNER、TABLE_NAME、および KEY_SEQ の値で並べ替えられます。The results returned are ordered by TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, and KEY_SEQ.
アクセス許可Permissions
スキーマに対する SELECT 権限が必要です。Requires SELECT permission on the schema.
例Examples
次の例では、HumanResources.Department
データベースの AdventureWorks2012
テーブルの主キーを取得します。The following example retrieves the primary key for the HumanResources.Department
table in the AdventureWorks2012
database.
USE AdventureWorks2012;
GO
EXEC sp_pkeys @table_name = N'Department'
,@table_owner = N'HumanResources';
例: Azure Synapse AnalyticsAzure Synapse Analytics、Parallel Data WarehouseParallel Data WarehouseExamples: Azure Synapse AnalyticsAzure Synapse Analytics and Parallel Data WarehouseParallel Data Warehouse
次の例では、DimAccount
データベースの AdventureWorksPDW2012
テーブルの主キーを取得します。The following example retrieves the primary key for the DimAccount
table in the AdventureWorksPDW2012
database. このメソッドは、テーブルに主キーがないことを示すゼロ行を返します。It returns zero rows indicating that the table does not have a primary key.
-- Uses AdventureWorks
EXEC sp_pkeys @table_name = N'DimAccount';
参照See Also
Transact-sql)(のカタログストアドプロシージャ Catalog Stored Procedures (Transact-SQL)
システム ストアド プロシージャ (Transact-SQL)System Stored Procedures (Transact-SQL)