Rediger

Del via


Add context for GitHub Copilot in SQL Server Management Studio

Add more context to your prompts in GitHub Copilot in SQL Server Management Studio (SSMS) by using references, in addition to its implicit, contextual knowledge, to obtain better responses.

Implicit context

GitHub Copilot in SSMS automatically provides context to the prompt based on the active query editor and its database connection. The following information is implicitly included in the chat context:

  • The currently selected text in the active query editor.
  • The current file active in the query editor.
  • The database connection for the active query editor.

Depending on your prompt, Copilot reads the contents of the active query editor, or reads the selected text in the editor.

Reference other files

You can ask your T-SQL related questions in natural language and GitHub Copilot Chat answers in the context of active editor in SSMS. References provide specific information you want Copilot to consider when answering your question.

By selecting other files open in the editor, you might be able to form better questions without having to write out or paste long pieces of information. Specifying the context also enables Copilot to provide you with more relevant answers.

To reference a file in GitHub Copilot Chat, add a # symbol at the beginning of the file name. For example, if you have a file named GetSalesInfo.sql, refer to it in the chat as #GetSalesInfo.sql.

Use slash commands for code assistance

GitHub Copilot in SSMS offers code assistance to help you set the intent for common database development tasks. This intent can accelerate productivity to help you understand and fix queries faster. Highlight any query in the editor and use the slash commands to document, explain, fix, or optimize the query. The slash commands are available in both the chat window and inline chat.

Command Usage
/doc Add comments for selected T-SQL code to help the next person who looks at your code understand what you did.
/explain Get in-depth details about the selected T-SQL code.
/fix Address errors in the T-SQL code and return a correct version to review, including an explanation of the issue and what was changed.
/help Get help on using Copilot Chat.
/optimize Identify anti-patterns in the query and make changes consistent with T-SQL best practices

Manage chat history context with threads

As you iterate and send multiple chat prompts in a chat session, Copilot uses the history of chat prompts and responses. The previous prompts and responses provide context to your current chat prompt. This means that you can ask follow-up questions or clarify your previous question without having to repeat the context. For example, you can ask "How do I change the compatibility mode?", "What are the differences between compatibility mode 140 and 170?", "What do I need to consider when changing compatibility mode?", and more.

To start over with a new chat session and discard the current context, select Delete thread. Select Create new thread to start a new thread in the chat view. New threads are useful when you want to move to a different topic and avoid the previous context and history. Use threads to keep conversations focused on the task at hand, and keep the context clear so the answers are based on relevant history.

Best practices

Copilot Chat uses the chat history to get context about your request. To give Copilot only the relevant history:

  • Use threads to start a new conversation for a new task.
  • Delete requests that are no longer relevant or that didn't give you the desired result.

Keep the chat conversation open and continue to iterate and prompt Copilot to improve the suggested solution. Copilot has both the context of the generated code and your current conversation history. As you keep asking questions, Copilot further refines the response according to your requirements.