Lesson 2 of 2 External AI Models

Creating and Managing External Models

This lesson explains CREATE EXTERNAL MODEL, its options and credentials, local models, permissions and model lifecycle management.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the options of CREATE EXTERNAL MODEL.
  • Identify the permissions for creating and using external models.
  • Explain how ALTER EXTERNAL MODEL supports endpoint changes.

CREATE EXTERNAL MODEL

An external model stores an endpoint's LOCATION (an HTTPS URL), its API_FORMAT (Azure OpenAI, OpenAI, Ollama or ONNX Runtime), the MODEL_TYPE (EMBEDDINGS), the MODEL name, an optional CREDENTIAL and optional PARAMETERS appended to every request. External models are available in SQL Server 2025, Azure SQL Database, SQL database in Fabric and Azure SQL Managed Instance with the Always-up-to-date policy.

CREATE DATABASE SCOPED CREDENTIAL [https://contoso-ai.openai.azure.com]
WITH IDENTITY = 'Managed Identity', SECRET = '{"resourceid":"https://cognitiveservices.azure.com"}';

CREATE EXTERNAL MODEL EmbeddingModel
WITH (
    LOCATION   = 'https://contoso-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://contoso-ai.openai.azure.com],
    PARAMETERS = '{"dimensions":1536, "sql_rest_options":{"retry_count":3}}'
);

The credential's name must match the scheme and host of the location URL, and its path must be the same or more general. sql_rest_options.retry_count (0 to 10) retries transient HTTP failures. In SQL Server 2025, the external rest endpoint enabled server option must be turned on; Azure SQL Database and SQL database in Fabric enable it by default.

Spot the bug

An external model that cannot authenticate

Exam skills 3.1.2

Find the lines that stop this external model from calling the endpoint.

Log in to try this activity Interactive activities are free with an account.

Local models with ONNX Runtime and Ollama

SQL Server 2025 can also use models that run locally, which keeps data on the server and avoids per-call costs. With API_FORMAT = 'ONNX Runtime' on Windows, the model and tokenizer files sit in a local folder, LOCAL_RUNTIME_PATH points to the ONNX Runtime libraries, and the external AI runtimes enabled option and PREVIEW_FEATURES are required. With API_FORMAT = 'Ollama', the location points to a locally hosted Ollama embedding endpoint. Only trusted models should be used, because a malicious model could compromise the server.

True or false

Local models

Exam skills 3.1.2

Decide whether each statement about local models in SQL Server 2025 is true.

Log in to try this activity Interactive activities are free with an account.

Permissions and lifecycle

The permissions and statements for managing external models are summarized in the following table.

TaskStatement or permission
Create a modelCREATE EXTERNAL MODEL (or ALTER ANY EXTERNAL MODEL) permission
Use a model in AI functionsGRANT EXECUTE ON EXTERNAL MODEL::name
Change endpoint, credential or parametersALTER EXTERNAL MODEL name SET (...)
Remove a modelDROP EXTERNAL MODEL (fails while a schema-bound view references it)
List modelsSELECT * FROM sys.external_models

Because code refers to the model by name, ALTER EXTERNAL MODEL can move every caller to a new deployment, region or credential without code changes, as long as the new deployment returns vectors of the same model and dimensions. Moving to a different embedding model is a migration: re-embed into a new column or table, switch queries, then drop the old vectors. The ai_generate_embeddings_summary extended event helps troubleshoot failing calls in SQL Server.

Scenario questions

Managing a model

Exam skills 3.1.2

Choose the statement for each lifecycle change.

Log in to try this activity Interactive activities are free with an account.
Worked example

Moving to a new deployment

An external model points to an Azure OpenAI deployment in a region that is being decommissioned. A new resource in another region has a deployment of the same text-embedding-3-small model. Twenty procedures call AI_GENERATE_EMBEDDINGS with the model name. What is the least disruptive change?

Create a credential for the new endpoint (managed identity, with the server's identity granted Cognitive Services OpenAI User on the new resource) and run ALTER EXTERNAL MODEL to set the new location and credential. The procedures keep using the same name, and existing vectors stay valid because the model and dimensions are unchanged. Creating a second model would require changing all twenty procedures, and re-embedding is not necessary for the same model.

Check yourself

Which model types can CREATE EXTERNAL MODEL register?

Show answer

EMBEDDINGS; the API formats are Azure OpenAI, OpenAI, Ollama and ONNX Runtime.

Exam Tips

Remember for the exam

  • Register an endpoint: CREATE EXTERNAL MODEL with LOCATION, API_FORMAT, MODEL_TYPE, MODEL, CREDENTIAL.
  • Use permission: EXECUTE ON EXTERNAL MODEL.
  • Switch endpoint without code changes: ALTER EXTERNAL MODEL.
  • Local models on SQL Server 2025: ONNX Runtime or Ollama API formats.