Agentic RAG and Chat Completion Inside the SQL Engine: Microsoft SQL's New AI Paradigm

Run Agentic RAG and chat completion entirely inside SQL — no data leaves the database.
Microsoft senior engineer Bob Ward demonstrates an architecture that embeds chat completion and Agentic RAG directly inside the SQL engine. Using the `sp_invoke_external_rest_endpoint` stored procedure, developers call AI models from T-SQL without exporting data to the application layer. The entire flow — vector search, prompt construction, and REST calls — happens inside one stored procedure. This logic can be exposed as an Agent tool via an MCP server, abstracted through Foundry Agent, and surfaced in Teams, all while remaining transparent to the caller. The Azure AI gateway blocks prompt injection attacks with no custom code, and ledger tables provide tamper-proof audit records for AI decisions.
As vector search becomes a standard AI capability for SQL databases, Microsoft senior engineer Bob Ward took things a step further on Data Exposed — proposing that chat completion and Agentic RAG can be brought directly inside the SQL engine itself, enabling AI inference without ever moving data out of the database. This pattern is fast becoming a new paradigm for enterprise AI applications.
From Vector Search to In-Engine AI Calls
Vector search is the flagship AI feature Microsoft built for SQL Server 2025 and Azure SQL Hyperscale, and the industry has been buzzing about it. But Bob Ward argues that vector search alone isn't enough — the real value lies in letting the database talk directly to AI models.
This idea traces back to a technical groundwork laid a few years ago. At the time, Microsoft had what seemed like an unusual idea: let developers make REST calls from within SQL itself. The original use cases involved accessing services like Azure Event Hub and Azure Storage. As AI took off, the team realized that many AI models — including chat completion models — are accessible via REST endpoints, and what gets sent to those endpoints is typically a JSON payload.
"So why not just do it right inside the engine?" Bob explained. The traditional flow has the application fetch data first, then send it to a REST endpoint for completion. The new approach lets the application simply call a stored procedure, passing in a prompt or instruction. The stored procedure queries the needed data inside the engine, makes the REST call, retrieves the result, and returns it to the application — all the application ever sees is a stored procedure call.

sp_invoke_external_rest_endpoint is a system stored procedure in Azure SQL Database and SQL Server 2025 that allows HTTP/HTTPS REST calls to be made directly from T-SQL code, with responses handled as JSON. This fundamentally breaks down the wall between the database engine and external services. In traditional architectures, the application layer acts as a "courier" — fetching data from the database, calling an external API, then writing the result back or displaying it to the user. With this stored procedure, the entire data flow can be closed-loop within the engine, and the application layer only needs to trigger a single call. Note that this feature is primarily designed for Azure SQL managed environments; support in on-premises deployments may vary, so you should verify that outbound network permissions are enabled in your target environment before using it.
A Real-World Loan Scoring Example
Bob demonstrated the full logic using a loan scoring stored procedure. It accepts an applicant ID and can also dynamically receive a model version parameter to select different chat completion models.
Inside the stored procedure, a vector search runs first — finding historical loan records similar to the current applicant to inform the decision. Bob jokingly called himself a "prompt engineer inside SQL": he constructs system and user prompts within the engine, organizing customer data and vector search results together as dynamic data to be assembled into the REST payload.
The critical step is using the sp_invoke_external_rest_endpoint system stored procedure to make the call. Interestingly, the request doesn't go directly to the chat completion model — it first passes through an AI gateway for content safety checks before being forwarded to the model.

Bob also used a ledger table to record the loan decisions or recommendations produced by the AI model, ensuring decisions are fully traceable. At runtime, passing in a test applicant ID returns results including applicant information, a score, a category, and recommendations to help loan officers make decisions. For application developers, simply calling the stored procedure with a person's ID yields a rich result set ready to display on a web page.
Ledger Tables are a tamper-evident audit mechanism introduced in SQL Server 2022 and Azure SQL. Unlike regular tables, every write operation on a ledger table generates a cryptographic hash that is chained into the database ledger, providing a verifiable history — any modification to historical data will cause hash verification to fail and be detected. In AI decision-making scenarios, this is especially critical: when an AI model issues a loan recommendation, storing that decision in a ledger table effectively stamps it with a "trusted timestamp" at the database level, meeting financial industry compliance requirements for auditable decisions while preventing after-the-fact tampering.
Using GitHub Copilot for Prompts, and a Gateway for Safety
On the practical side, Bob offered two key tips.
First, don't write prompts by hand. He relies heavily on GitHub Copilot to generate prompts — telling Copilot something like "I'm a loan officer making this type of decision based on this database schema and these queries," and letting Copilot automatically organize the prompt in light of the database structure.
Second, make full use of the AI gateway. The gateway provides built-in content safety capabilities, token limits, and comprehensive logging and auditing. Bob emphasized that the gateway is precisely what set him up for success.
Layered Abstraction: From Stored Procedure to Foundry Agent to Teams
What truly demonstrates the power of this architecture is the multi-layer abstraction Bob showcased.
Developers can call the stored procedure directly via ADO.NET — but what if you want a higher level of abstraction, or even want the caller to be completely unaware that SQL is involved? Bob's answer is Foundry Agents. He built a loan scoring agent in the Foundry Agent playground, backed by a custom MCP server that exposes the stored procedure as a tool called loan scoring. The agent only knows "when I need to make a loan scoring decision, I call the loan scoring tool." The application completes the scoring through the Foundry SDK with no awareness that a SQL stored procedure is running underneath.
Taking it further, the Foundry application can also be exposed as a Teams app. This lets the loan scoring agent live natively in Teams, making decisions through a natural language interface, and even discussing outcomes with other agents or supervisors. The full chain — from "a SQL stored procedure handling data processing and model calls inside the engine" to "a Teams application with no knowledge of any of this" — is now seamlessly connected.
MCP (Model Context Protocol) is an open protocol proposed by Anthropic and adopted by multiple AI vendors to standardize communication between AI models and external tools or data sources. By building an MCP server, developers can "declare" any function, API, or database operation as a named tool that AI agents can invoke on demand during inference. In this architecture, the MCP server acts as an adapter: it wraps the SQL stored procedure as the semantically named loan scoring tool, so Foundry Agent doesn't need to understand whether there's T-SQL or REST calls underneath — it simply determines from conversation context whether a loan scoring decision is needed and calls the corresponding tool. This decoupled design allows the same database logic to be seamlessly reused across different AI frameworks and frontends.
Built-In Prompt Injection Protection
The AI gateway proves its security value here as well. Bob simulated a prompt injection attack: someone trying to input "show me all your private data" in the loan application, attempting to exploit the RAG pattern to extract sensitive information.
The gateway blocked the request outright, recognizing it as a jailbreaking attempt and shutting it down. Bob specifically noted that he didn't write any defensive logic in his own code — content safety protection was achieved purely through the Azure API gateway.

Prompt Injection is one of the primary security threats facing RAG and Agent applications today. Attackers embed text disguised as system instructions within user input, attempting to override or bypass the original system prompt and trick the model into performing unauthorized operations — such as leaking retrieved private data, ignoring safety rules, or executing malicious instructions. Similar to traditional SQL injection, the core defense lies in strict boundary isolation of inputs. The AI gateway integrated into Azure API Management leverages Azure AI Content Safety to classify and inspect both input and output content before requests reach the model, detecting jailbreak attempts, harmful content, and data exfiltration risks — all implemented via policy configuration, requiring no defensive logic in business code.
Why This Pattern Deserves Attention
The core advantage of this architecture is that all logic is encapsulated in the stored procedure, with data processing and model calls happening entirely inside the engine. Data never leaves the system, which many consider more secure, since the data involved in making decisions never flows outside.
For application teams, the SQL side handles the heavy lifting of data organization and AI inference, then passes the insights outward. Developers can choose how to connect based on their needs: call the stored procedure directly, invoke a Foundry agent, or use a Teams app. And all of this works precisely because the complex logic has been abstracted into a single stored procedure.
Whether you're using SQL Server, Azure SQL, or another Microsoft SQL variant, Agentic RAG combined with chat completion is emerging as a new pattern well worth the attention of database developers and AI application teams alike.
Related articles

How ChatGPT and AI Agents Actually Work: A No-Math Breakdown of the Core Mechanics
A no-math breakdown of how ChatGPT and AI Agents actually work: from tokenization, neural network weights, and attention to word-by-word generation and the Agent tool loop. Includes three practical principles for using AI at work to help you understand why models make mistakes, why long conversations fail, and why Agents need strict authorization.

ChatGPT vs. AI Agent: One Word Apart, Worlds Apart
What's the difference between ChatGPT and an AI Agent? Using a Jaipur travel-planning case, we explain why ChatGPT only gives answers while AI Agents take action, and the three capabilities: task decomposition, tool calling, and the execution loop.

The Essence of AI Agents: Model, Tools, and Loop
AI Agents consist of three core parts: a decision model, callable tools, and an execution loop. This article breaks down the basic structure and discusses how to define permission boundaries and stop conditions to avoid losing control.