
Introduction
Imagine you’re a DBA managing hundreds of SQL Server troubleshooting articles, internal runbooks, incident reports, and performance-tuning documents.
A developer asks:
“Our production database suddenly has high CPU. What should I check first?”
A general-purpose AI model might suggest common troubleshooting steps. But what if you want the answer to use your organization’s troubleshooting procedures, past incidents, and approved technical documentation?
This is where Retrieval-Augmented Generation (RAG) becomes useful.
RAG retrieves relevant information from your own data and provides it to a large language model (LLM) as context for generating an answer. SQL Server can act as the source of that information, the store for embeddings, and the retrieval engine in a RAG solution.
In this practical guide, we’ll explain RAG in simple terms, build a small SQL Server knowledge base, explore the retrieval process using T-SQL, and see how the retrieved information is passed to an AI model.
1. What exactly is RAG?
RAG stands for Retrieval-Augmented Generation. Let’s break the name into three parts.
Retrieval
Find relevant information from a knowledge base, database, documents, or other approved sources.
Augmentation
Add the retrieved information to the original question as context for the AI model.
Generation
The language model uses the question and supplied context to generate a response.
For example, imagine that your company has an internal document explaining how to troubleshoot SQL Server blocking.
When someone asks, “Why are my database sessions waiting?”, a RAG application retrieves the relevant troubleshooting instructions and provides them to the language model. The model then uses those instructions to explain what to investigate.
The model doesn’t need to have learned your private document during its original training. Your application supplies the relevant information at answer time.
2. RAG vs. a traditional AI chatbot
| Traditional AI chatbot | RAG-powered chatbot |
|---|---|
| Primarily uses the model’s existing knowledge and the prompt | Retrieves relevant information from your selected data sources |
| May not know your latest internal procedures | Can use updated documents when the retrieval index is refreshed |
| May give a generic troubleshooting answer | Can use your team’s specific troubleshooting procedures |
| Answers may be difficult to verify | Can return source references alongside the answer |
RAG does not guarantee that every answer will be correct. If retrieval returns irrelevant content or the model misinterprets the context, the answer can still be wrong. Good retrieval, source tracking, and answer validation remain important.
Also, RAG is not the same as fine-tuning. Fine-tuning changes model behavior through additional training; RAG supplies relevant data at inference time.
3. How does RAG work?
A typical RAG application follows this process:
Step 1: Prepare the knowledge base
Articles, runbooks, support tickets, and database records
Step 2: Chunk and generate embeddings
Split large documents into useful sections and represent each section as a vector
Step 3: Store and retrieve
Search for content relevant to the user’s question
Step 4: Send context to the LLM
Combine the question with the retrieved passages
Step 5: Generate the answer
Return an answer, ideally with supporting sources
The first two steps usually prepare or update the knowledge base. The remaining steps happen when a user asks a question.
Let’s explore the SQL Server implementation.
4. How SQL Server fits into a RAG solution
SQL Server can hold the original content, metadata, and vector embeddings. It can also retrieve relevant records using SQL queries and vector functions. An external language model can then generate the final answer.
Microsoft documents RAG patterns using the SQL Database Engine with Azure OpenAI, Azure AI Search, and native vector functionality.
There are three common architectures:
Option 1: SQL Server 2025 with native vector capabilities
Store embeddings in VECTOR columns and use functions such as VECTOR_DISTANCE to retrieve similar content. SQL Server 2025 also provides AI integration features for configured embedding models and external REST calls.
Option 2: SQL Server with Azure AI Search
Keep operational data in SQL Server and use Azure AI Search for document indexing and retrieval. This can be useful when you need a dedicated search service and richer search pipelines.
Option 3: SQL Server with an application layer
Use Python, C#, or another application to generate embeddings, retrieve data, and call the LLM. This is also a practical option for SQL Server versions that don’t have native vector support.
The right choice depends on your SQL Server version, data volume, search requirements, security boundaries, and operational budget.
5. Build a simple SQL Server RAG example
Let’s build a small knowledge base containing SQL Server troubleshooting information.
We’ll use SQL Server 2025 for the native vector examples. The example demonstrates retrieval, not a complete production deployment.
Step 1: Create a knowledge base
Each row will represent a short piece of troubleshooting guidance.
CREATE TABLE dbo.DBAKnowledge
(
ChunkID INT IDENTITY(1,1) PRIMARY KEY,
Topic NVARCHAR(200) NOT NULL,
Content NVARCHAR(MAX) NOT NULL,
SourceName NVARCHAR(300) NOT NULL,
Embedding VECTOR(1536) NULL
);
GO
INSERT INTO dbo.DBAKnowledge
(Topic, Content, SourceName)
VALUES
(
N'High CPU',
N'When SQL Server CPU usage is high, check Query Store
for expensive queries. Review execution plans,
CPU time, logical reads, and recent plan changes.',
N'High CPU Troubleshooting Runbook'
),
(
N'Blocking',
N'When sessions are blocked, identify the blocking
session, inspect wait information, and review the
transaction and query involved before taking action.',
N'Blocking Troubleshooting Runbook'
),
(
N'Slow Queries',
N'For slow queries, inspect execution plans, index
usage, statistics, logical reads, and Query Store
history before changing indexes or rewriting SQL.',
N'Query Performance Runbook'
);
GO
We have three knowledge chunks. In a real system, these could be sections extracted from hundreds or thousands of internal documents.
The Embedding column is nullable because we haven’t generated the vectors yet. The dimension of 1,536 is an example; it must match your chosen embedding model.
Step 2: Generate embeddings
In SQL Server 2025, you can configure an external embedding model using CREATE EXTERNAL MODEL and call AI_GENERATE_EMBEDDINGS. The model endpoint, credentials, model name, and permissions must be configured correctly first.
Conceptually, after configuring an external model named DBAEmbeddingModel, you can generate and store the embeddings:
UPDATE dbo.DBAKnowledge
SET Embedding =
AI_GENERATE_EMBEDDINGS
(
Content USE MODEL DBAEmbeddingModel
)
WHERE Embedding IS NULL;
GO
This assumes the configured model returns 1,536-dimensional vectors. Test the model call on a small number of rows first, and use batching and error handling for larger datasets.
For documents, generate embeddings for each chunk rather than embedding an entire large document as one record.
Step 3: Generate an embedding for the user’s question
Suppose the user asks:
What should I investigate when SQL Server CPU usage increases?
The application or SQL Server AI integration must generate a vector for this question using the same compatible embedding model.
With the configured SQL Server 2025 model, the query can be represented like this:
DECLARE @Question NVARCHAR(MAX) =
N'What should I investigate when SQL Server CPU usage increases?';
DECLARE @QuestionVector VECTOR(1536);
SET @QuestionVector =
AI_GENERATE_EMBEDDINGS
(
@Question USE MODEL DBAEmbeddingModel
);
The resulting vector represents the question numerically. It can now be compared with the vectors stored for the knowledge chunks.
Step 4: Retrieve the most relevant chunks
Use VECTOR_DISTANCE to compare the question vector with each stored embedding.
SELECT TOP (3)
ChunkID,
Topic,
Content,
SourceName,
VECTOR_DISTANCE
(
'cosine',
@QuestionVector,
Embedding
) AS Distance
FROM dbo.DBAKnowledge
WHERE Embedding IS NOT NULL
ORDER BY Distance ASC;
This example uses cosine distance. For this distance metric, a lower distance indicates a closer match.
The query might return the high-CPU runbook first, followed by related query-performance guidance. The exact ordering depends on the actual embeddings and content; it should not be assumed without running the example.
Why does this matter? The application can retrieve relevant passages even when the user’s wording differs from the wording in the original documents.
For SQL Server 2025, VECTOR_DISTANCE supports exact distance comparisons. Vector index and approximate search features have separate availability and configuration requirements, so verify the current documentation before using them in production.
6. Send the retrieved context to the AI model
This is the step that turns semantic search into a RAG application.
SQL Server has retrieved the relevant content. Your application then builds a prompt containing the user’s question and the retrieved passages.
For example:
Example prompt sent to the LLM
You are a SQL Server troubleshooting assistant. Answer the user’s question using the supplied context. If the context is insufficient, say so. Do not invent internal procedures. Question: What should I investigate when SQL Server CPU usage increases? Retrieved context: 1. High CPU Troubleshooting Runbook: Check Query Store for expensive queries. Review execution plans, CPU time, logical reads, and recent plan changes. 2. Query Performance Runbook: Inspect execution plans, index usage, statistics, logical reads, and Query Store history. Instructions: Provide a prioritized checklist and identify which source supports each recommendation.
Copy prompt
Your application sends this prompt to a configured chat model, such as an appropriate OpenAI or Azure OpenAI model, and displays the generated response.
The LLM call is a separate operation from the retrieval query. It can be made from the application, or integrated with supported SQL Server external REST functionality. SQL Server does not automatically send every query result to an LLM just because vectors are stored in a table.
7. Can the entire RAG workflow run inside T-SQL?
For supported platforms, a RAG workflow can be orchestrated largely or entirely through T-SQL: retrieve content, process vectors, construct a request, and call an external model endpoint.
Microsoft documents this pattern using native database functionality and sp_invoke_external_rest_endpoint for external AI calls.
However, there is an important distinction:
AI_GENERATE_EMBEDDINGSgenerates embeddings through a configured model.VECTOR_DISTANCEcalculates distance between vectors.sp_invoke_external_rest_endpointcan call a supported external REST API.- The chat or generation model produces the natural-language answer.
These functions do different jobs. You still need to configure endpoints, authentication, request payloads, permissions, and error handling.
For many applications, keeping the orchestration in Python or C# is easier to maintain. For SQL-centric environments, a T-SQL-led design may reduce application changes.
8. What about structured SQL Server data?
RAG is not limited to documents.
Imagine you have a table containing support tickets:
| TicketID | Issue | Resolution |
|---|---|---|
| 101 | High CPU after deployment | Identified a query plan regression |
| 102 | Sessions blocked for several minutes | Investigated a long-running transaction |
| 103 | Report query takes too long | Reviewed indexes and statistics |
You can create searchable text from selected columns and generate embeddings for it. When someone asks a question in natural language, the system retrieves similar tickets.
But do not use vector search for every database question.
If the user asks, “How many support tickets were opened this month?”, a normal SQL aggregation is more appropriate than an embedding search.
For structured data, a useful design often combines:
- Traditional SQL queries for exact values, counts, filters, and joins.
- Vector search for semantic similarity.
- The LLM to interpret retrieved information and explain results.
Microsoft also recommends combining RAG with other techniques when working with structured data.
9. Production considerations DBAs should not ignore
A working demonstration is only the beginning. Before deploying RAG, consider these areas.
Security and permissions
- Give the application access only to approved tables, views, or stored procedures.
- Apply row-level security and other existing access controls where appropriate.
- Avoid sending confidential data to an external AI service unless the data handling is approved.
- Store credentials securely and audit access to sensitive information.
Never assume that retrieving a document through RAG automatically enforces the permissions of the person asking the question. The retrieval layer must enforce authorization before context reaches the model.
Data freshness
When a source document changes, its embedding may need to be regenerated. Track the source version, last-updated timestamp, and embedding status.
Performance and cost
- Start with a small
TOPvalue, such as 3 or 5, and measure retrieval quality. - Avoid regenerating embeddings unnecessarily.
- Batch embedding operations and handle transient API errors.
- Evaluate exact search versus approximate vector indexes as the dataset grows.
- Monitor query performance, external endpoint latency, token usage, and total cost.
Answer quality
Retrieval quality matters as much as the choice of LLM. Test the system with real user questions, verify the retrieved chunks, and measure whether the final answer is grounded in the sources.
10. Common RAG mistakes
| Mistake | Better approach |
|---|---|
| Embedding huge documents as a single item | Split documents into meaningful chunks |
| Returning too many irrelevant passages | Tune retrieval, filters, and the number of results |
| Treating every LLM answer as correct | Validate answers against retrieved sources |
| Ignoring permissions during retrieval | Enforce access controls before building the prompt |
| Using vectors for exact numeric questions | Use normal SQL for exact calculations and filters |
| Failing to refresh changed content | Track source versions and regenerate embeddings |
| Assuming vector indexes are available everywhere | Verify version, platform, and feature status |
Conclusion
Retrieval-Augmented Generation provides a practical way to connect generative AI with your organization’s own data.
For SQL Server professionals, the key idea is simple: SQL Server retrieves the relevant knowledge, and the language model uses that knowledge to formulate an answer.
You can start with a small troubleshooting knowledge base, generate embeddings for a few documents, retrieve the closest matches with VECTOR_DISTANCE, and pass the results to an LLM.
Once that works, expand the solution with chunking, hybrid search, access controls, source citations, monitoring, and automated updates.
Discover more from Technology with Vivek Johari
Subscribe to get the latest posts sent to your email.


