|
| 1 | +SQL Server 2025 introduces a new set of AI and vector functions that enable database developers to integrate AI-powered capabilities directly into T-SQL. These new capabilities make it possible to generate embeddings, calculate vector similarity, and search across AI-enriched data without leaving SQL Server. This level of integration reduces the need for external services, simplifies application architecture, and supports real-time intelligent workloads. |
| 2 | + |
| 3 | +## AI and Vector Functions Overview |
| 4 | + |
| 5 | +The new AI features in SQL Server 2025 fall into three main categories: AI generation, vector operations, and vector indexing and search. |
| 6 | + |
| 7 | +### AI Generation Functions |
| 8 | + |
| 9 | +- **AI_GENERATE_CHUNKS** – Splits large text or documents into semantically coherent chunks that can later be embedded or stored for retrieval-augmented generation (RAG) scenarios. |
| 10 | +- **AI_GENERATE_EMBEDDINGS** – Generates embeddings from text input using an external model registered in SQL Server. These embeddings can be stored in tables for use in vector search, similarity analysis, or semantic ranking. |
| 11 | + |
| 12 | +### Vector Operations |
| 13 | + |
| 14 | +- **VECTOR_DISTANCE** – Computes the distance between two vector values, supporting distance metrics such as cosine, Euclidean, and dot product. |
| 15 | +- **VECTOR_NORM** – Returns the vector norm (magnitude) for a given vector. |
| 16 | +- **VECTOR_NORMALIZE** – Returns a normalized version of a vector, typically used before comparison or similarity searches. |
| 17 | +- **VECTORPROPERTY** – Returns metadata about a vector, such as its dimensions or element type. |
| 18 | + |
| 19 | +### External Models and Vector Indexes |
| 20 | + |
| 21 | +SQL Server 2025 allows you to register and manage external AI models using T-SQL. |
| 22 | +- **CREATE EXTERNAL MODEL / ALTER EXTERNAL MODEL / DROP EXTERNAL MODEL** – Manage AI models that are hosted locally or through supported model providers. |
| 23 | +- **CREATE VECTOR INDEX** – Creates an index optimized for vector data to accelerate similarity searches. |
| 24 | +- **VECTOR_SEARCH** – Performs similarity search operations on vector data using the vector index, returning the closest matches based on the selected distance metric. |
| 25 | + |
| 26 | +These capabilities allow SQL Server to serve as a foundation for retrieval-augmented generation, recommendation engines, and semantic search applications entirely within the database engine. |
| 27 | + |
| 28 | +### Half-precision vector storage and binary ingest |
| 29 | + |
| 30 | +Vectors can now use **half-precision floating-point (fp16)** elements to reduce memory usage and improve scan performance in embedding-heavy workloads. |
| 31 | +You can also **bulk-load vectors** in binary format using `BULK INSERT` or `OPENROWSET(BULK ...)`, which simplifies importing large embedding sets created outside SQL Server. |
| 32 | + |
| 33 | +## Example Scenario: Building a Product Recommendation Query |
| 34 | + |
| 35 | +Imagine you work for a retail company that stores product descriptions in a SQL Server 2025 database. The marketing team wants to build a recommendation feature that suggests products semantically similar to a selected item. Using the new AI and vector features, you can generate embeddings for product descriptions, store them in a table, and perform similarity searches without external processing. |
| 36 | + |
| 37 | +### Create and Register the Model |
| 38 | + |
| 39 | +Before generating embeddings, you must register an external model. |
| 40 | + |
| 41 | +```sql |
| 42 | +CREATE EXTERNAL MODEL embedding_model |
| 43 | +FROM OPENAI |
| 44 | +WITH (ENDPOINT = 'https://api.openai.com/v1/embeddings', |
| 45 | + API_KEY = SECRET('openai_key'), |
| 46 | + MODEL_NAME = 'text-embedding-3-small'); |
| 47 | +``` |
| 48 | + |
| 49 | +### Generate and Store Embeddings |
| 50 | + |
| 51 | +Once the model is registered, you can generate embeddings for your product descriptions and store them in a new table. |
| 52 | + |
| 53 | +```sql |
| 54 | +CREATE TABLE ProductEmbeddings |
| 55 | +( |
| 56 | + ProductID INT PRIMARY KEY, |
| 57 | + Description NVARCHAR(MAX), |
| 58 | + Embedding VECTOR(1536) |
| 59 | +); |
| 60 | + |
| 61 | +INSERT INTO ProductEmbeddings (ProductID, Description, Embedding) |
| 62 | +SELECT ProductID, |
| 63 | + Description, |
| 64 | + AI_GENERATE_EMBEDDINGS('embedding_model', Description) |
| 65 | +FROM Products; |
| 66 | +``` |
| 67 | + |
| 68 | +### Create a Vector Index and Run a Search |
| 69 | + |
| 70 | +To improve search performance, create a vector index to speed up similarity searches. |
| 71 | + |
| 72 | +```sql |
| 73 | +CREATE VECTOR INDEX idx_ProductEmbedding |
| 74 | +ON ProductEmbeddings (Embedding) |
| 75 | +WITH (DISTANCE_METRIC = 'cosine'); |
| 76 | +``` |
| 77 | + |
| 78 | +Now you can perform a semantic search for related products: |
| 79 | + |
| 80 | +```sql |
| 81 | +DECLARE @query NVARCHAR(MAX) = 'waterproof hiking backpack'; |
| 82 | +DECLARE @vector VECTOR(1536) = AI_GENERATE_EMBEDDINGS('embedding_model', @query); |
| 83 | + |
| 84 | +SELECT TOP 5 ProductID, Description, |
| 85 | + VECTOR_DISTANCE(Embedding, @vector, 'cosine') AS SimilarityScore |
| 86 | +FROM ProductEmbeddings |
| 87 | +ORDER BY SimilarityScore ASC; |
| 88 | +``` |
| 89 | + |
| 90 | +### Results |
| 91 | + |
| 92 | +| ProductID | Description | SimilarityScore | |
| 93 | +|------------|--------------|----------------| |
| 94 | +| 105 | "Lightweight waterproof travel backpack" | 0.07 | |
| 95 | +| 116 | "Hiking pack with rain cover and hydration slot" | 0.10 | |
| 96 | +| 117 | "Compact outdoor day pack with water resistance" | 0.12 | |
| 97 | +| 101 | "Trail-ready backpack with external straps" | 0.15 | |
| 98 | +| 119 | "Travel and camping waterproof duffel" | 0.18 | |
| 99 | + |
| 100 | +This example demonstrates how to integrate an external AI model, generate embeddings directly within T-SQL, and perform a similarity search using built-in vector functions. Everything runs inside SQL Server, which simplifies development and allows intelligent workloads to remain secure and governed under existing database policies. |
| 101 | + |
| 102 | +## Summary |
| 103 | + |
| 104 | +SQL Server 2025 introduces native AI capabilities that allow developers to build intelligent database applications directly in T-SQL. Functions such as `AI_GENERATE_EMBEDDINGS`, `VECTOR_DISTANCE`, and `VECTOR_SEARCH` streamline integration with AI models while maintaining performance and security. Together, these features make SQL Server 2025 a strong platform for semantic search, recommendations, and context-aware analytics without relying on external compute pipelines. |
0 commit comments