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
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:
| Dimension | What to Evaluate | Trade-off |
|---|---|---|
| Modality | Text-only, multimodal (text+image+audio), or embedding models | Multimodal models are more versatile but larger and more expensive |
| Language | Monolingual (English) or multilingual (100+ languages) | Multilingual models handle global data but may be less precise in each language |
| Size | Small (fast, cheap), medium (balanced), large (most capable) | Larger models produce better results but cost more per token and have higher latency |
| Structured output | Can 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 Category | Examples | Tokens/sec | Best For |
|---|---|---|---|
| Small | GPT-4o-mini, Phi-3 | Very fast | Classification, simple extraction, embeddings |
| Medium | GPT-4o | Fast | General Q&A, summarisation, code generation |
| Large | GPT-4, o1-series | Slower | Complex reasoning, multi-step analysis |
| Embedding | text-embedding-3-small/large | Very fast | Vector 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_endpointto 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:
- 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.
- Customer support summarisation β summarise complaint emails in English and Japanese. She chooses a multilingual medium model (GPT-4o) for balanced quality and language support.
- 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.
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.