Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Restituisci chiavi primarie da un server collegato in SQL Server (esempi T-SQL)

In SQL Server puoi usare sp_primarykeys stored procedure di sistema per restituire le colonne della chiave primaria da un server collegato specificato. Restituisce una riga per colonna chiave, per la tabella remota specificata.

Il modo più semplice per eseguire questa procedura memorizzata consiste nel passare il nome del server collegato. In questo modo verranno restituite tutte le chiavi primarie dal database predefinito sul server collegato specificato.

Hai anche la possibilità di specificare un database diverso e/o uno schema di tabella specifico.

Sintassi

La sintassi è questa:

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

Il @table_server argomento è l'unico argomento richiesto. Questo è il nome del server collegato da cui vuoi le informazioni sulla chiave primaria.

Gli altri argomenti sono facoltativi.

Esempio 1:restituire tutte le chiavi primarie nel database predefinito

L'esempio seguente restituisce tutte le chiavi primarie dal database predefinito sul server collegato chiamato Homer.

EXEC sp_primarykeys @table_server = 'Homer';

Può anche essere eseguito in questo modo:

EXEC sp_primarykeys 'Homer';

Risultato:

+-------------+---------------+--------------+---------------+-----------+-----------+
| 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      |
+-------------+---------------+--------------+---------------+-----------+-----------+

In questo caso ci sono quattro colonne di chiave primaria (sono elencate sotto COLUMN_NAME ). Noterai che il PK_NAME la colonna è NULL . Questa colonna è per l'identificatore della chiave primaria. Microsoft afferma che questo restituisce NULL se non applicabile all'origine dati.

Il KEY_SEQ colonna è il numero di sequenza della colonna in una chiave primaria multicolonna. In questo caso non c'erano chiavi primarie multicolonna, quindi il valore è 1 per ogni chiave primaria. L'esempio successivo restituisce alcuni risultati con chiavi primarie multicolonna.

Esempio 2:specificare un database diverso

L'esempio seguente specifica che WideWorldImportersDW dovrebbe essere utilizzato il database.

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

Risultato:

+----------------------+---------------+-------------------------+------------------------------+-----------+-----------+
| 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      |
+----------------------+---------------+-------------------------+------------------------------+-----------+-----------+

Esempio 3:specificare uno schema di tabella

L'esempio seguente restringe i risultati a uno schema di tabella specifico.

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

Risultati:

+----------------------+---------------+---------------+-------------------+-----------+-----------+
| 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      |
+----------------------+---------------+---------------+-------------------+-----------+-----------+