Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Renvoyer les clés primaires d'un serveur lié dans SQL Server (exemples T-SQL)

Dans SQL Server, vous pouvez utiliser les sp_primarykeys procédure stockée système pour renvoyer les colonnes de clé primaire à partir d'un serveur lié spécifié. Il renvoie une ligne par colonne clé, pour la table distante spécifiée.

Le moyen le plus simple d'exécuter cette procédure stockée consiste à transmettre le nom du serveur lié. Cela renverra toutes les clés primaires de la base de données par défaut sur le serveur lié spécifié.

Vous avez également la possibilité de spécifier une base de données différente et/ou un schéma de table spécifique.

Syntaxe

La syntaxe ressemble à ceci :

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

Le @table_server argument est le seul argument requis. Il s'agit du nom du serveur lié dont vous souhaitez obtenir les informations de clé primaire.

Les autres arguments sont facultatifs.

Exemple 1 - Renvoyer toutes les clés primaires dans la base de données par défaut

L'exemple suivant renvoie toutes les clés primaires de la base de données par défaut sur le serveur lié appelé Homer.

EXEC sp_primarykeys @table_server = 'Homer';

Il peut également être exécuté comme ceci :

EXEC sp_primarykeys 'Homer';

Résultat :

+-------------+---------------+--------------+---------------+-----------+-----------+
| TABLE_CAT   | TABLE_SCHEM   | TABLE_NAME   | COLUMN_NAME   | KEY_SEQ   | PK_NAME   |
|-------------+---------------+--------------+---------------+-----------+-----------|
| Music       | dbo           | Albums       | AlbumId       | 1         | NULL      |
| Music       | dbo           | Artists      | ArtistId      | 1         | NULL      |
| Music       | dbo           | Country      | CountryId     | 1         | NULL      |
| Music       | dbo           | Genres       | GenreId       | 1         | NULL      |
+-------------+---------------+--------------+---------------+-----------+-----------+

Dans ce cas, il y a quatre colonnes de clé primaire (elles sont répertoriées sous COLUMN_NAME ). Vous remarquerez que le PK_NAME la colonne est NULL . Cette colonne est pour l'identificateur de clé primaire. Microsoft indique que cela renvoie NULL s'il n'est pas applicable à la source de données.

Le KEY_SEQ colonne est le numéro de séquence de la colonne dans une clé primaire multicolonne. Dans ce cas, il n'y avait pas de clés primaires multicolonnes, donc la valeur est 1 pour chaque clé primaire. L'exemple suivant renvoie des résultats avec des clés primaires multicolonnes.

Exemple 2 - Spécifier une autre base de données

L'exemple suivant spécifie que le WideWorldImportersDW base de données doit être utilisée.

EXEC sp_primarykeys 
  @table_server = 'Homer',   
  @table_catalog = 'WideWorldImportersDW';

Résultat :

+----------------------+---------------+-------------------------+------------------------------+-----------+-----------+
| TABLE_CAT            | TABLE_SCHEM   | TABLE_NAME              | COLUMN_NAME                  | KEY_SEQ   | PK_NAME   |
|----------------------+---------------+-------------------------+------------------------------+-----------+-----------|
| WideWorldImportersDW | Dimension     | City                    | City Key                     | 1         | NULL      |
| WideWorldImportersDW | Dimension     | Customer                | Customer Key                 | 1         | NULL      |
| WideWorldImportersDW | Dimension     | Date                    | Date                         | 1         | NULL      |
| WideWorldImportersDW | Dimension     | Employee                | Employee Key                 | 1         | NULL      |
| WideWorldImportersDW | Dimension     | Payment Method          | Payment Method Key           | 1         | NULL      |
| WideWorldImportersDW | Dimension     | Stock Item              | Stock Item Key               | 1         | NULL      |
| WideWorldImportersDW | Dimension     | Supplier                | Supplier Key                 | 1         | NULL      |
| WideWorldImportersDW | Dimension     | Transaction Type        | Transaction Type Key         | 1         | NULL      |
| WideWorldImportersDW | Fact          | Movement                | Movement Key                 | 1         | NULL      |
| WideWorldImportersDW | Fact          | Movement                | Date Key                     | 2         | NULL      |
| WideWorldImportersDW | Fact          | Order                   | Order Key                    | 1         | NULL      |
| WideWorldImportersDW | Fact          | Order                   | Order Date Key               | 2         | NULL      |
| WideWorldImportersDW | Fact          | Purchase                | Purchase Key                 | 1         | NULL      |
| WideWorldImportersDW | Fact          | Purchase                | Date Key                     | 2         | NULL      |
| WideWorldImportersDW | Fact          | Sale                    | Sale Key                     | 1         | NULL      |
| WideWorldImportersDW | Fact          | Sale                    | Invoice Date Key             | 2         | NULL      |
| WideWorldImportersDW | Fact          | Stock Holding           | Stock Holding Key            | 1         | NULL      |
| WideWorldImportersDW | Fact          | Transaction             | Transaction Key              | 1         | NULL      |
| WideWorldImportersDW | Fact          | Transaction             | Date Key                     | 2         | NULL      |
| WideWorldImportersDW | Integration   | City_Staging            | City Staging Key             | 1         | NULL      |
| WideWorldImportersDW | Integration   | Customer_Staging        | Customer Staging Key         | 1         | NULL      |
| WideWorldImportersDW | Integration   | Employee_Staging        | Employee Staging Key         | 1         | NULL      |
| WideWorldImportersDW | Integration   | ETL Cutoff              | Table Name                   | 1         | NULL      |
| WideWorldImportersDW | Integration   | Lineage                 | Lineage Key                  | 1         | NULL      |
| WideWorldImportersDW | Integration   | Movement_Staging        | Movement Staging Key         | 1         | NULL      |
| WideWorldImportersDW | Integration   | Order_Staging           | Order Staging Key            | 1         | NULL      |
| WideWorldImportersDW | Integration   | PaymentMethod_Staging   | Payment Method Staging Key   | 1         | NULL      |
| WideWorldImportersDW | Integration   | Purchase_Staging        | Purchase Staging Key         | 1         | NULL      |
| WideWorldImportersDW | Integration   | Sale_Staging            | Sale Staging Key             | 1         | NULL      |
| WideWorldImportersDW | Integration   | StockHolding_Staging    | Stock Holding Staging Key    | 1         | NULL      |
| WideWorldImportersDW | Integration   | StockItem_Staging       | Stock Item Staging Key       | 1         | NULL      |
| WideWorldImportersDW | Integration   | Supplier_Staging        | Supplier Staging Key         | 1         | NULL      |
| WideWorldImportersDW | Integration   | Transaction_Staging     | Transaction Staging Key      | 1         | NULL      |
| WideWorldImportersDW | Integration   | TransactionType_Staging | Transaction Type Staging Key | 1         | NULL      |
+----------------------+---------------+-------------------------+------------------------------+-----------+-----------+

Exemple 3 - Spécifier un schéma de table

L'exemple suivant limite les résultats à un schéma de table spécifique.

EXEC sp_primarykeys 
  @table_server = 'Homer',
  @table_schema = 'Fact',
  @table_catalog = 'WideWorldImportersDW';

Résultats :

+----------------------+---------------+---------------+-------------------+-----------+-----------+
| TABLE_CAT            | TABLE_SCHEM   | TABLE_NAME    | COLUMN_NAME       | KEY_SEQ   | PK_NAME   |
|----------------------+---------------+---------------+-------------------+-----------+-----------|
| WideWorldImportersDW | Fact          | Movement      | Movement Key      | 1         | NULL      |
| WideWorldImportersDW | Fact          | Movement      | Date Key          | 2         | NULL      |
| WideWorldImportersDW | Fact          | Order         | Order Key         | 1         | NULL      |
| WideWorldImportersDW | Fact          | Order         | Order Date Key    | 2         | NULL      |
| WideWorldImportersDW | Fact          | Purchase      | Purchase Key      | 1         | NULL      |
| WideWorldImportersDW | Fact          | Purchase      | Date Key          | 2         | NULL      |
| WideWorldImportersDW | Fact          | Sale          | Sale Key          | 1         | NULL      |
| WideWorldImportersDW | Fact          | Sale          | Invoice Date Key  | 2         | NULL      |
| WideWorldImportersDW | Fact          | Stock Holding | Stock Holding Key | 1         | NULL      |
| WideWorldImportersDW | Fact          | Transaction   | Transaction Key   | 1         | NULL      |
| WideWorldImportersDW | Fact          | Transaction   | Date Key          | 2         | NULL      |
+----------------------+---------------+---------------+-------------------+-----------+-----------+