Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Zurückgeben von Primärschlüsseln von einem Verbindungsserver in SQL Server (T-SQL-Beispiele)

In SQL Server können Sie die sp_primarykeys verwenden gespeicherte Systemprozedur, um die Primärschlüsselspalten von einem angegebenen Verbindungsserver zurückzugeben. Es gibt eine Zeile pro Schlüsselspalte für die angegebene entfernte Tabelle zurück.

Die einfachste Möglichkeit, diese gespeicherte Prozedur auszuführen, besteht darin, den Namen des Verbindungsservers zu übergeben. Dadurch werden alle Primärschlüssel aus der Standarddatenbank auf dem angegebenen Verbindungsserver zurückgegeben.

Sie haben auch die Möglichkeit, eine andere Datenbank und/oder ein bestimmtes Tabellenschema anzugeben.

Syntax

Die Syntax sieht so aus:

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

Der @table_server Argument ist das einzige erforderliche Argument. Dies ist der Name des Verbindungsservers, von dem Sie die Primärschlüsselinformationen erhalten möchten.

Die anderen Argumente sind optional.

Beispiel 1 – Alle Primärschlüssel in der Standarddatenbank zurückgeben

Das folgende Beispiel gibt alle Primärschlüssel aus der Standarddatenbank auf dem Verbindungsserver namens Homer zurück.

EXEC sp_primarykeys @table_server = 'Homer';

Es kann auch so ausgeführt werden:

EXEC sp_primarykeys 'Homer';

Ergebnis:

+-------------+---------------+--------------+---------------+-----------+-----------+
| 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 diesem Fall gibt es vier Primärschlüsselspalten (sie sind unter COLUMN_NAME aufgelistet ). Sie werden feststellen, dass der PK_NAME Spalte ist NULL . Diese Spalte ist für die Primärschlüsselkennung. Microsoft gibt an, dass dies NULL zurückgibt, wenn es nicht auf die Datenquelle zutrifft.

Der KEY_SEQ Spalte ist die Sequenznummer der Spalte in einem mehrspaltigen Primärschlüssel. In diesem Fall gab es keine mehrspaltigen Primärschlüssel, also ist der Wert 1 für jeden Primärschlüssel. Das nächste Beispiel gibt einige Ergebnisse mit mehrspaltigen Primärschlüsseln zurück.

Beispiel 2 – Geben Sie eine andere Datenbank an

Das folgende Beispiel gibt an, dass die Datei WideWorldImportersDW Datenbank verwendet werden soll.

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

Ergebnis:

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

Beispiel 3 – Geben Sie ein Tabellenschema an

Das folgende Beispiel grenzt die Ergebnisse auf ein bestimmtes Tabellenschema ein.

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

Ergebnisse:

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