Domain 3 β€” Module 1 of 7 14%
22 of 28 overall
Domain 3: Implement AI Capabilities in Database Solutions Free ⏱ ~12 min read

External AI Models: Choose, Create, Manage

Evaluate and integrate external AI models into your SQL database β€” understand model types, sizes, languages, structured output, and how to create and manage model definitions.

Bringing AI into your database

Simple explanation

Think of hiring a specialist consultant.

Your database is great at storing and querying data. But it cannot understand natural language, recognise images, or generate text β€” that is not its job. External AI models are specialists you hire: one understands English and Japanese, another can read images, and a third generates structured JSON from questions. You register them in your database so your T-SQL code can call them directly.

The key decision: which specialist do you hire for each task? A cheap, fast one for simple jobs? A powerful, expensive one for complex analysis? This module teaches you how to choose.

Evaluating external models

When choosing a model for your SQL-based AI solution, evaluate across four dimensions:

Four evaluation dimensions for external models
DimensionWhat to EvaluateTrade-off
ModalityText-only, multimodal (text+image+audio), or embedding modelsMultimodal models are more versatile but larger and more expensive
LanguageMonolingual (English) or multilingual (100+ languages)Multilingual models handle global data but may be less precise in each language
SizeSmall (fast, cheap), medium (balanced), large (most capable)Larger models produce better results but cost more per token and have higher latency
Structured outputCan the model return JSON, XML, or structured formats reliably?Essential for database integration β€” unstructured text is hard to INSERT into tables

Model size guide

Size CategoryExamplesTokens/secBest For
SmallGPT-4o-mini, Phi-3Very fastClassification, simple extraction, embeddings
MediumGPT-4oFastGeneral Q&A, summarisation, code generation
LargeGPT-4, o1-seriesSlowerComplex reasoning, multi-step analysis
Embeddingtext-embedding-3-small/largeVery fastVector search, semantic similarity
Exam tip: Structured output is critical for SQL integration

When calling an AI model from T-SQL (via sp_invoke_external_rest_endpoint), you need the response in a parseable format β€” typically JSON. Models that reliably produce structured JSON output are essential. If the model returns free-form text, you cannot reliably extract values into table columns.

Look for models that support JSON mode or structured output schemas in their API. The exam may present a scenario where the model returns inconsistent formats β€” the fix is configuring structured output in the API call.

Creating external models in SQL

External models are database objects that register an AI endpoint:

-- Step 1: Create a database-scoped credential with the endpoint URL as its name
CREATE DATABASE SCOPED CREDENTIAL [https://vaultbank-ai.openai.azure.com/]
    WITH IDENTITY = 'HTTPEndpointHeaders',
         SECRET = '{"api-key": "your-api-key-here"}';

-- Step 2: Create an external model definition
CREATE EXTERNAL MODEL EmbeddingModel
WITH (
    LOCATION = 'https://vaultbank-ai.openai.azure.com/openai/deployments/text-embedding-3-small/embeddings?api-version=2024-10-21',
    API_FORMAT = 'Azure OpenAI',
    MODEL_TYPE = EMBEDDINGS,
    MODEL = 'text-embedding-3-small',
    CREDENTIAL = [https://vaultbank-ai.openai.azure.com/]
);

Note: Only the EMBEDDINGS model type is currently documented for CREATE EXTERNAL MODEL. For completions (text generation, Q&A), use sp_invoke_external_rest_endpoint to call the Azure OpenAI chat completions API directly.

Managing models

-- View all registered models
SELECT * FROM sys.external_models;

-- Alter model (e.g., change model version)
ALTER EXTERNAL MODEL EmbeddingModel
SET (MODEL = 'text-embedding-3-large');

-- Drop model
DROP EXTERNAL MODEL EmbeddingModel;

Security for model endpoints

Model endpoints require authentication. Options:

  • API keys stored as database-scoped credentials
  • Managed Identity (preferred for Azure-hosted databases)
  • Key Vault references for centralised secret management
-- Using managed identity (no secrets needed)
CREATE DATABASE SCOPED CREDENTIAL [https://vaultbank-ai.openai.azure.com/]
    WITH IDENTITY = 'Managed Identity',
         SECRET = '{"resourceid": "https://cognitiveservices.azure.com"}';

CREATE EXTERNAL MODEL SecureModel
WITH (
    LOCATION = 'https://vaultbank-ai.openai.azure.com/openai/deployments/text-embedding-3-small/embeddings?api-version=2024-10-21',
    API_FORMAT = 'Azure OpenAI',
    MODEL_TYPE = EMBEDDINGS,
    MODEL = 'text-embedding-3-small',
    CREDENTIAL = [https://vaultbank-ai.openai.azure.com/]
);
Scenario: Priya's model selection at Vault Bank

Priya needs three AI capabilities for the banking platform:

  1. Fraud detection β€” classify transactions as suspicious or legitimate. She chooses a small model (GPT-4o-mini) because it is fast and cheap for binary classification at high volume.
  2. Customer support summarisation β€” summarise complaint emails in English and Japanese. She chooses a multilingual medium model (GPT-4o) for balanced quality and language support.
  3. Semantic search β€” find similar customer queries. She chooses an embedding model (text-embedding-3-small) for generating vector representations.

Three different models, three different use cases β€” each optimised for its task.

Question

What is an external model in SQL Server?

Click or press Enter to reveal answer

Answer

A database object (CREATE EXTERNAL MODEL) that registers an AI model endpoint β€” like Azure OpenAI or Microsoft Foundry. It stores the endpoint URL, deployment name, authentication, and model type so T-SQL code can invoke the model directly.

Click to flip back

Question

Why is structured output important for SQL + AI integration?

Click or press Enter to reveal answer

Answer

When calling AI models from T-SQL, responses must be parseable to extract values into table columns. Structured output (JSON mode) ensures the model returns consistent, machine-readable formats instead of free-form text that is difficult to parse reliably.

Click to flip back

Question

What are the main model types used in SQL AI solutions?

Click or press Enter to reveal answer

Answer

For CREATE EXTERNAL MODEL, only EMBEDDINGS (generate vector representations for semantic search) is currently documented. For text generation (completions, Q&A, summarisation), use sp_invoke_external_rest_endpoint to call the Azure OpenAI API directly. Choose based on use case: search uses embeddings via external models, RAG completions use the REST endpoint.

Click to flip back

Knowledge Check

Leo at SearchWave needs to add semantic product search. The system must generate vector embeddings for 2 million product descriptions nightly, and latency per embedding must be under 50ms. Which model choice is best?

Next up: Embeddings: Design, Chunk, and Generate β€” turn your data into vectors that AI can search and compare.