We are excited to announce the General Availability (GA) of AI_GENERATE_EMBEDDINGS and CREATE EXTERNAL MODEL in Azure SQL Database and Azure SQL Managed Instance.
These two T-SQL features — CREATE EXTERNAL MODEL and AI_GENERATE_EMBEDDINGS — work together as a single, integrated pipeline for generating vector embeddings directly from T-SQL. No data movement, no external orchestration, no application-layer pipeline required.
EXTERNAL MODEL object defines where to get embeddings from and how to authenticate — registered once, reused everywhere. AI_GENERATE_EMBEDDINGS calls the external model to generate the vector arrays inline in any T-SQL statement. Developers then store those vectors in a VECTOR-typed column within a table and query them using VECTOR_DISTANCE or VECTOR_SEARCH functions. Together, these capabilities form the foundation for Retrieval-Augmented Generation (RAG) and vector similarity search, built entirely within the SQL engine you are already familiar with.
Generating vector embeddings using an external AI model provider
Here is how CREATE EXTERNAL MODEL and AI_GENERATE_EMBEDDINGS connect in a single workflow:
-- Once: register your provider-specific REST endpoint as a named database object
CREATE EXTERNAL MODEL MyEmbeddingModel
WITH (LOCATION = '...', API_FORMAT = 'Azure OpenAI', MODEL_TYPE = EMBEDDINGS, MODEL = '...');
-- Anywhere: generate embeddings inline in T-SQL
UPDATE docs
SET docs.embedding = AI_GENERATE_EMBEDDINGS(docs.content USE MODEL MyEmbeddingModel)
FROM documents AS docs;
CREATE EXTERNAL MODEL
CREATE EXTERNAL MODEL is a T-SQL DDL statement that creates a named object in the database storing the location, API format, model type, and authentication credential for an AI model inference endpoint. After the external model object is created, AI_GENERATE_EMBEDDINGS can use the external model object to generate embeddings.
Syntax:
CREATE EXTERNAL MODEL external_model_object_name
[ AUTHORIZATION owner_name ]
WITH
( LOCATION = '<prefix>://<path>[:<port>]'
, API_FORMAT = 'Azure OpenAI | OpenAI'
, MODEL_TYPE = EMBEDDINGS
, MODEL = 'model-name'
[ , CREDENTIAL = <credential_name> ]
[ , PARAMETERS = '{"valid":"JSON"}' ]
);
Supported API_FORMAT values: Azure OpenAI, and OpenAI. The only accepted MODEL_TYPE at GA is EMBEDDINGS. The optional PARAMETERS JSON sets model-level defaults, including retry count: '{"sql_rest_options":{"retry_count":3}}'.
AI_GENERATE_EMBEDDINGS
AI_GENERATE_EMBEDDINGS is a built-in scalar-valued T-SQL function that references a registered external model, calls its configured embedding endpoint, and returns a JSON array value containing the generated embeddings. It accepts any character expression (nvarchar, varchar, nchar, or char) and can be used in SELECT, INSERT, UPDATE, and MERGE statements, including within procedural code such as stored procedures and triggers.
Syntax:
AI_GENERATE_EMBEDDINGS (
source
USE MODEL model_identifier
[ PARAMETERS optional_json_request_body_parameters ]
)
The optional PARAMETERS JSON allows the caller to override model defaults or pass other parameter values to the AI model inference endpoint. For example, the Azure OpenAI REST API supports an optional dimensions parameter that controls the number of dimensions returned — when supported by the model, PARAMETERS can specify a different value, for example '{"dimensions":256}'.
GA Capability Summary
| Platform availability | Azure SQL Database, Azure SQL Managed Instance (Always-up-to-date and SQL Server 2025 update policies), SQL Server 2025 |
|---|---|
| Embedding providers | Azure OpenAI, OpenAI |
| Authentication | API key via headers (HTTPEndpointHeaders), API key via query string (HTTPEndpointQueryString), Managed Identity, Shared Access Signature Token |
| Model registration | CREATE EXTERNAL MODEL — registered once, referenced by name in all AI function calls |
| Retry logic | Built-in exponential backoff for transient HTTP errors; configurable via sql_rest_options at model level (PARAMETERS on CREATE EXTERNAL MODEL) or at query level (PARAMETERS on AI_GENERATE_EMBEDDINGS) |
| Permissions | CREATE EXTERNAL MODEL requires CREATE EXTERNAL MODEL or ALTER ANY EXTERNAL MODEL. Calling AI_GENERATE_EMBEDDINGS requires EXECUTE ON EXTERNAL MODEL::ModelName |
| Telemetry | Extended Events: ai_generate_embeddings_summary (status code, errors, model name) and external_rest_endpoint_summary (additional REST detail); performance counters |
| Complimentary features | AI_GENERATE_CHUNKS for text chunking; VECTOR data type for storage; VECTOR_DISTANCE for exact similarity; VECTOR_SEARCH for approximate nearest-neighbor search (preview) |
Getting Started
The following walkthrough sets up native AI embedding generation end-to-end using Azure OpenAI with API key authentication — the simplest path to get started. Code samples follow the CREATE EXTERNAL MODEL and AI_GENERATE_EMBEDDINGS reference documentation on Microsoft Learn.
Step 1: Enable external REST endpoint invocation
This one-time configuration is required on Azure SQL Managed Instance and SQL Server 2025 before the functions can make outbound REST calls to your embedding endpoint. It is not required on Azure SQL Database.
EXECUTE sp_configure 'external rest endpoint enabled', 1;
RECONFIGURE WITH OVERRIDE;
GO
Step 2: Create a database master key
A database master key must exist before you can create database-scoped credentials. Skip this step if one already exists.
IF NOT EXISTS (SELECT * FROM sys.symmetric_keys
WHERE [name] = '##MS_DatabaseMasterKey##')
BEGIN
CREATE MASTER KEY ENCRYPTION BY PASSWORD = N'<strong-password>';
END
GO
Step 3: Create a credential for your Azure OpenAI endpoint
Store your Azure OpenAI API key as a database-scoped credential. The credential name must be the base URL of your Azure OpenAI resource — in square brackets, with no query string — because CREATE EXTERNAL MODEL uses this name to match credentials to endpoints. Replace <my-azure-openai-endpoint> with your actual Azure OpenAI resource name.
CREATE DATABASE SCOPED CREDENTIAL [https://<my-azure-openai-endpoint>.cognitiveservices.azure.com/]
WITH IDENTITY = 'HTTPEndpointHeaders',
SECRET = '{"api-key": "<your-azure-openai-api-key>"}';
GO
Step 4: Register your embedding model with CREATE EXTERNAL MODEL
This is the CREATE EXTERNAL MODEL step — run it once to register your Azure OpenAI embedding deployment as a named object in the database. All subsequent AI_GENERATE_EMBEDDINGS calls reference this name. Replace <my-azure-openai-endpoint> with your resource name and text-embedding-ada-002 with your actual deployment name.
CREATE EXTERNAL MODEL MyEmbeddingModel
WITH (
LOCATION = 'https://<my-azure-openai-endpoint>.cognitiveservices.azure.com/openai/deployments/text-embedding-ada-002/embeddings?api-version=2024-02-01',
API_FORMAT = 'Azure OpenAI',
MODEL_TYPE = EMBEDDINGS,
MODEL = 'text-embedding-ada-002',
CREDENTIAL = [https://<my-azure-openai-endpoint>.cognitiveservices.azure.com/]
);
GO
Step 5: Grant permission to use the model
By default, only the model owner can call AI_GENERATE_EMBEDDINGS against it. Grant EXECUTE on the specific model to any other principal that needs to generate embeddings.
-- Permission to create external models (for other users who need to register models)
GRANT CREATE EXTERNAL MODEL TO [<principal>];
GO
-- Permission to use this specific model in AI_GENERATE_EMBEDDINGS
GRANT EXECUTE ON EXTERNAL MODEL::MyEmbeddingModel TO [<principal>];
GO
Step 6: Create your source and destination tables
Create a documents table to hold the text you want to embed, and a document_embeddings table with a VECTOR(1536) column to store the generated embeddings. The dimension count (1536) matches text-embedding-ada-002 — adjust this number if your model outputs a different number of dimensions.
-- Source table
CREATE TABLE documents
(
id INT IDENTITY(1,1) PRIMARY KEY,
content NVARCHAR(MAX)
);
GO
-- Destination table for chunk text and embeddings
CREATE TABLE document_embeddings
(
embeddings_id INT IDENTITY(1,1) PRIMARY KEY,
chunk_text NVARCHAR(100),
vector_embeddings VECTOR(1536)
);
GO
Step 7: Insert your source text
Insert the text you want to embed into the documents table. Replace these sample rows with your own data.
INSERT INTO documents (content)
VALUES
('Your first document or text passage goes here.'),
('Your second document or text passage goes here.');
GO
Step 8: Generate and store embeddings with AI_GENERATE_EMBEDDINGS
Use AI_GENERATE_CHUNKS to split the content column into fixed-size chunks, then call AI_GENERATE_EMBEDDINGS referencing the model registered in Step 4. The chunk text and its vector array are inserted directly into document_embeddings.
INSERT INTO document_embeddings (chunk_text, vector_embeddings)
SELECT c.chunk,
AI_GENERATE_EMBEDDINGS(c.chunk USE MODEL MyEmbeddingModel)
FROM documents AS t
CROSS APPLY AI_GENERATE_CHUNKS(
SOURCE = t.content,
CHUNK_TYPE = FIXED,
CHUNK_SIZE = 100
) AS c;
GO
Verify the results:
SELECT * FROM document_embeddings;
Tip: Configuring retry count
If an embeddings call encounters transient HTTP errors (such as 429 Too Many Requests), the function retries automatically. Retry count can be set at the model level (applies to all calls) or overridden at query time. The value must be a whole number between 0 and 10.
Model level — in CREATE EXTERNAL MODEL PARAMETERS:
'{"sql_rest_options":{"retry_count":3}}'Query level — in AI_GENERATE_EMBEDDINGS PARAMETERS:
'{"sql_rest_options":{"retry_count":10}}'
-- Query-level retry override example
DECLARE @params JSON = N'{"sql_rest_options":{"retry_count":10}}'
SELECT id,
AI_GENERATE_EMBEDDINGS(content USE MODEL MyEmbeddingModel PARAMETERS @params)
FROM documents;
Summary
AI_GENERATE_EMBEDDINGS and CREATE EXTERNAL MODEL are now generally available in Azure SQL Database and Azure SQL Managed Instance. CREATE EXTERNAL MODEL registers your AI endpoint as a named database object; AI_GENERATE_EMBEDDINGS securely calls that endpoint to generate vector embeddings directly from T-SQL. At GA, the pipeline includes built-in retry logic for transient errors. Vectors are stored in VECTOR-typed columns and queried using SQL’s native vector search capabilities — no external pipeline, no data movement required. Try it today and let us know what you build.
Full reference documentation on Microsoft Learn: CREATE EXTERNAL MODEL · AI_GENERATE_EMBEDDINGS
Tags: azure sql database · azure sql managed instance · sql server 2025 · create external model · ai · embeddings · vector search · rag · t-sql
0 comments
Be the first to start the discussion.