Di SQL Server Anda dapat menggunakan sp_primarykeys
prosedur tersimpan sistem untuk mengembalikan kolom kunci utama dari server tertaut yang ditentukan. Ini mengembalikan satu baris per kolom kunci, untuk tabel jarak jauh yang ditentukan.
Cara paling sederhana untuk menjalankan prosedur tersimpan ini adalah dengan meneruskan nama server yang ditautkan. Melakukannya akan mengembalikan semua kunci utama dari database default pada server tertaut yang ditentukan.
Anda juga memiliki opsi untuk menentukan database yang berbeda dan/atau skema tabel tertentu.
Sintaks
Sintaksnya seperti ini:
sp_primarykeys [ @table_server = ] 'table_server' [ , [ @table_name = ] 'table_name' ] [ , [ @table_schema = ] 'table_schema' ] [ , [ @table_catalog = ] 'table_catalog' ]
@table_server
argumen adalah satu-satunya argumen yang diperlukan. Ini adalah nama server tertaut yang Anda inginkan dari informasi kunci utama.
Argumen lainnya adalah opsional.
Contoh 1 – Mengembalikan semua Kunci Utama di Basis Data Default
Contoh berikut mengembalikan semua kunci utama dari database default pada server tertaut yang disebut Homer.
EXEC sp_primarykeys @table_server = 'Homer';
Itu juga dapat dijalankan seperti ini:
EXEC sp_primarykeys 'Homer';
Hasil:
+-------------+---------------+--------------+---------------+-----------+-----------+ | 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 | +-------------+---------------+--------------+---------------+-----------+-----------+
Dalam hal ini ada empat kolom kunci utama (terdaftar di bawah COLUMN_NAME
). Anda akan melihat bahwa PK_NAME
kolomnya adalah NULL
. Kolom ini untuk pengidentifikasi kunci utama. Microsoft menyatakan bahwa ini mengembalikan NULL jika tidak berlaku untuk sumber data.
KEY_SEQ
kolom adalah nomor urut kolom dalam kunci utama multikolom. Dalam hal ini tidak ada kunci utama multikolom, jadi nilainya adalah 1
untuk setiap kunci utama. Contoh berikutnya mengembalikan beberapa hasil dengan kunci utama multikolom.
Contoh 2 – Tentukan Database yang Berbeda
Contoh berikut menetapkan bahwa WideWorldImportersDW
database harus digunakan.
EXEC sp_primarykeys @table_server = 'Homer', @table_catalog = 'WideWorldImportersDW';
Hasil:
+----------------------+---------------+-------------------------+------------------------------+-----------+-----------+ | 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 | +----------------------+---------------+-------------------------+------------------------------+-----------+-----------+
Contoh 3 – Tentukan Skema Tabel
Contoh berikut mempersempit hasil ke skema tabel tertentu.
EXEC sp_primarykeys @table_server = 'Homer', @table_schema = 'Fact', @table_catalog = 'WideWorldImportersDW';
Hasil:
+----------------------+---------------+---------------+-------------------+-----------+-----------+ | 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 | +----------------------+---------------+---------------+-------------------+-----------+-----------+