Sqlserver
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Sqlserver

Trả lại các khóa chính từ một máy chủ được liên kết trong SQL Server (Ví dụ T-SQL)

Trong SQL Server, bạn có thể sử dụng sp_primarykeys hệ thống lưu trữ thủ tục để trả về các cột khóa chính từ một máy chủ được liên kết cụ thể. Nó trả về một hàng cho mỗi cột khóa, cho bảng từ xa được chỉ định.

Cách đơn giản nhất để thực hiện thủ tục được lưu trữ này là chuyển tên của máy chủ được liên kết. Làm điều đó sẽ trả về tất cả các khóa chính từ cơ sở dữ liệu mặc định trên máy chủ được liên kết được chỉ định.

Bạn cũng có tùy chọn chỉ định một cơ sở dữ liệu khác và / hoặc một lược đồ bảng cụ thể.

Cú pháp

Cú pháp như sau:

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

@table_server đối số là đối số bắt buộc duy nhất. Đây là tên của máy chủ được liên kết mà bạn muốn có thông tin khóa chính.

Các đối số khác là tùy chọn.

Ví dụ 1 - Trả lại tất cả các Khóa chính trong Cơ sở dữ liệu mặc định

Ví dụ sau đây trả về tất cả các khóa chính từ cơ sở dữ liệu mặc định trên máy chủ được liên kết có tên là Homer.

EXEC sp_primarykeys @table_server = 'Homer';

Nó cũng có thể chạy như thế này:

EXEC sp_primarykeys 'Homer';

Kết quả:

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

Trong trường hợp này, có bốn cột khóa chính (chúng được liệt kê trong COLUMN_NAME ). Bạn sẽ nhận thấy rằng PK_NAME cột là NULL . Cột này dành cho mã định danh khóa chính. Microsoft tuyên bố rằng điều này trả về NULL nếu không áp dụng cho nguồn dữ liệu.

KEY_SEQ cột là số thứ tự của cột trong khóa chính nhiều cột. Trong trường hợp này không có khóa chính nhiều cột, vì vậy giá trị là 1 cho mỗi khóa chính. Ví dụ tiếp theo trả về một số kết quả với khóa chính nhiều cột.

Ví dụ 2 - Chỉ định một cơ sở dữ liệu khác

Ví dụ sau chỉ định rằng WideWorldImportersDW cơ sở dữ liệu nên được sử dụng.

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

Kết quả:

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

Ví dụ 3 - Chỉ định một lược đồ bảng

Ví dụ sau đây thu hẹp kết quả vào một lược đồ bảng cụ thể.

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

Kết quả:

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

  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Tích hợp CLR SQL Server có hỗ trợ tệp cấu hình không?

  2. Cách tốt nhất để kiểm tra kết nối SQL Server theo chương trình là gì?

  3. Các chỉ mục của SQL Server:Các yêu cầu chính, tác động đến hiệu suất và những cân nhắc

  4. Những hạn chế đối với SQL Server Compact là gì? (Hoặc - làm thế nào để người ta chọn một cơ sở dữ liệu để sử dụng trên nền tảng MS?)

  5. @@ TEXTSIZE trong SQL Server là gì?