共用方式為


在 SQL Server Management Studio(預覽版)中,使用 GitHub Copilot 執行資料庫指令。

資料庫指示為 SQL Server 管理工作室(SSMS)中的 GitHub Copilot 提供資料庫專屬的上下文與指引,這些內容直接存在於資料庫中。 資料庫擁有者與團隊可利用這些資料來描述商業規則、資料慣例及使用模式。 透過資料庫指令,Copilot 能在使用者提問或撰寫查詢時產生更準確、有意義且一致的回應。 資料庫指令作為活文件,隨資料庫一同移動,並在副駕駛互動時自動套用。

在本文中,您將學習如何使用資料庫指令,進一步賦能使用者與 Copilot 更快找到正確的物件與資訊。 此功能有助於減少向 Copilot 提供資料庫、業務及內部知識的時間。

有關 GitHub Copilot 聊天的相關資訊,請參閱「 在 SQL Server Management Studio 中使用 GitHub Copilot 聊天體驗(預覽版)」。

資料庫指令的運作方式

資料庫指令以元資料形式儲存在資料庫中,使 Copilot 能在執行時自動發現並套用指令。 當使用者在資料庫中與 GitHub Copilot 互動時,例如用自然語言提問或產生 T-SQL,Copilot 會將這些指令作為額外的上下文。 此情境確保回應反映業務定義、標準表格及領域特定規則,而非僅依賴結構名稱或推測。 指令會跨會話持續存在,且任何存取同一資料庫的使用者都能取得。

先決條件

SSMS 中 GitHub Copilot 的資料庫指令依賴於擴充屬性中的資訊。

  1. 安裝 SSMS 22.3 或更新版本,搭配 AI Assistance 工作負載

  2. 請用 Copilot 登入你的 GitHub 帳號。

免費使用 GitHub Copilot。 註冊並使用 AI 更快、更有效率地編碼。

使用 GitHub Copilot 來新增或查看資料庫指示

這些步驟展示了如何使用 GitHub Copilot 建立和使用資料庫指令。

問副駕駛一個問題,以揭示缺失的上下文

首先用自然語言向 Copilot 詢問你的資料問題。 例如, What was revenue for the last quarter of the year? 如果企業的會計年度與曆年起始日期不同,或收入需要計算多個資料表,相關表格或欄位是資料庫指令的良好候選對象。

將商業規則定義為資料庫指令

在 Copilot 聊天中,描述你希望 Copilot 記住的規則或說明。 例如, Add an instruction for the Finance.CompanyRevenue table to state that revenue is defined as SUM(NetAmount) excluding refunds recorded in Finance.Refunds.

將指令儲存到資料庫

一旦 Copilot 產生 T-SQL 語法,將指令作為擴充屬性加入,你必須將此資訊持久化為資料庫指令。 在詢問模式下,GitHub Copilot 無法對資料庫執行修改查詢。

-- Created by GitHub Copilot in SSMS - review carefully before executing
EXECUTE sp_addextendedproperty
    @name = N'AGENTS.md',
    @value = N'Revenue is defined as SUM(NetAmount) excluding refunds recorded in Finance.Refunds. When calculating actual revenue, refunds must be subtracted from the Revenue column values.',
    @level0type = N'SCHEMA',
    @level0name = N'Finance',
    @level1type = N'TABLE',
    @level1name = N'CompanyRevenue';

當你將指令加入資料庫後,Copilot 會自動將該資訊套用到未來的互動中。

使用Copilot,將指令套用

再問 Copilot 同樣或相關的問題, What was revenue for the last quarter of the year? Copilot 現在會用儲存的指令產生符合你業務收入定義的結果,無需你重新陳述規則。 你也可以請 Copilot Show me the query used to get this information. 確認 Copilot 是否理解該指示。

其他範例

資料庫物件需要套用商業規則的情境無限多,包括資料表、欄位和儲存程序。

  • The dbo.Apts table stores information about patient appointments
  • The Status column in dbo.Apts encodes status as: 1 = Scheduled, 2 = Completed, 5 = Confirmed, 99 = Cancelled
  • The DtCrtd column in dbo.Apts lists the date and time the appointment was created

使用 Copilot 的資料庫指令時,也可以詢問目前存在的指令。

  • Show me the database instructions for this database
  • Does this database have a constitution set?

使用 GitHub Copilot 建立資料庫架構

SSMS 中的 GitHub Copilot 也支援資料庫的單一架構,設定資料庫的最高優先順序指令。 章程中可以包含建議、編碼指引、保留政策等內容。

備註

實作資料庫規範適用於在 SSMS 中使用 GitHub Copilot 的所有使用者的 GitHub Copilot 對話。 這些建議與指引僅適用於 GitHub Copilot 的對話。

EXECUTE sp_addextendedproperty
    @name = N'CONSTITUTION.md',
    @value = N'Any T-SQL in this database must comply with the organizational standards and guidelines outlined in this constitution document.
 Queries that use SELECT * should not be used.';

需求

資料庫指令目前僅支援作為擴充屬性。 它們遵循 AGENTS.md 格式,提供 GitHub Copilot 所使用的上下文。 物件擴展屬性的名稱必須是 AGENTS.md。 物件只能存在一個被命名 AGENTS.md 的屬性。 如果指令錯誤,你可以用 sp_updateextendedproperty來更新,或用 sp_dropextendedproperty刪除。 資料庫指令不需要其他設定就能運作。