Skip to content

Commit 5f5f043

Browse files
author
Davide Mauri
authored
Clarified the result set (#35574)
1 parent 6e6dc5f commit 5f5f043

1 file changed

Lines changed: 47 additions & 46 deletions

File tree

docs/t-sql/functions/vector-search-transact-sql.md

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
title: "VECTOR_SEARCH (Transact-SQL)"
3-
description: "VECTOR_SEARCH search for vectors similar to a given query vectors using an approximate nearest neighbors vector search algorithm."
3+
description: VECTOR_SEARCH search for vectors similar to a given query vectors using an approximate nearest neighbors vector search algorithm.
44
author: yorek
55
ms.author: damauri
6-
ms.reviewer: damauri, mikeray
7-
ms.date: 08/11/2025
6+
ms.reviewer: mikeray, randolphwest
7+
ms.date: 10/14/2025
88
ms.service: sql
99
ms.subservice: t-sql
1010
ms.topic: reference
@@ -17,15 +17,15 @@ helpviewer_keywords:
1717
- "VECTOR_SEARCH function"
1818
- "vector, search"
1919
dev_langs:
20-
- "TSQL"
20+
- TSQL
2121
monikerRange: "=sql-server-ver17 || =sql-server-linux-ver17"
2222
---
2323

2424
# VECTOR_SEARCH (Transact-SQL) (Preview)
2525

2626
[!INCLUDE [sqlserver2025](../../includes/applies-to-version/sqlserver2025.md)]
2727

28-
Search for vectors similar to a given query vectors using an approximate nearest neighbors vector search algorithm. To learn more about how vector indexing and vector search works, and the differences between exact and approximate search, refer to [Vectors in the SQL Database Engine](../../sql-server/ai/vectors.md).
28+
Search for vectors similar to a given query vectors using an approximate nearest neighbors vector search algorithm. To learn more about how vector indexing and vector search works, and the differences between exact and approximate search, refer to [Vector search and vector indexes in the SQL Database Engine](../../sql-server/ai/vectors.md).
2929

3030
## Preview feature
3131

@@ -78,7 +78,16 @@ The alias is used to reference the result set.
7878

7979
## Return result set
8080

81-
The returned result set has all the columns from the table specified in TABLE argument, plus the extra `distance` column. The `distance` column contains the distance between the given vector in the COLUMN argument and the vector specified in SIMILAR_TO argument.
81+
The result set returned by the `VECTOR_SEARCH` function includes:
82+
83+
- All columns from the table specified in the `TABLE` argument.
84+
- An additional column named `distance`, which represents the distance between the vector in the column specified by the `COLUMN` argument and the vector provided in the `SIMILAR_TO` argument.
85+
86+
The distance column is generated by the `VECTOR_SEARCH` function itself, while all other columns come from the table referenced in the `TABLE` argument.
87+
88+
If you use an alias for the table in the `TABLE` argument, you must use that same alias to reference its columns in the `SELECT` statement. You can't use the alias assigned to `VECTOR_SEARCH` to reference columns from the table specified in `TABLE`. This behavior is easier to understand if you think of the result set as a JSON object that merges the output of `VECTOR_SEARCH` with the table data.
89+
90+
If the table specified in the `TABLE` argument already contains a column named `distance`, the behavior will be similar to a SQL join between two tables that share a column name. In such cases, you must use table aliases to disambiguate the column references—otherwise, an error will be raised.
8291

8392
## Limitations
8493

@@ -91,22 +100,18 @@ The current preview has the following limitations:
91100
Vector search happens before applying any predicate. Additional predicates are applied only after the most similar vectors are returned. The following sample returns the top 10 rows with embeddings most similar to the query vector `@qv`, then applies the predicate specified in the `WHERE` clause. If none of the 10 rows associated with the vectors returned by the vector search have the `accepted` column equal to 1, the result is empty.
92101

93102
```sql
94-
SELECT
95-
s.id,
96-
s.title,
97-
r.distance
98-
FROM
99-
VECTOR_SEARCH(
100-
TABLE = dbo.sessions AS s,
101-
COLUMN = embedding,
102-
SIMILAR_TO = @qv,
103-
METRIC = 'cosine',
104-
TOP_N = 10
105-
) AS r
106-
WHERE
107-
accepted = 1
108-
ORDER BY
109-
r.distance
103+
SELECT s.id,
104+
s.title,
105+
r.distance
106+
FROM VECTOR_SEARCH(
107+
TABLE = dbo.sessions AS s,
108+
COLUMN = embedding,
109+
SIMILAR_TO = @qv,
110+
METRIC = 'cosine',
111+
TOP_N = 10
112+
) AS r
113+
WHERE accepted = 1
114+
ORDER BY r.distance;
110115
```
111116

112117
### VECTOR_SEARCH can't be used in views
@@ -139,38 +144,34 @@ ORDER BY s.distance
139144
Same as example 1, but this time the query vectors are taking from another table instead of a variable.
140145

141146
```sql
142-
CREATE TABLE #t (
143-
id INT,
144-
q NVARCHAR(MAX),
145-
v VECTOR(1536)
147+
CREATE TABLE #t
148+
(
149+
id INT,
150+
q NVARCHAR (MAX),
151+
v VECTOR(1536)
146152
);
147-
INSERT INTO
148-
#t
149-
SELECT
150-
id, q, ai_generate_embeddings(q USE MODEL Ada2Embeddings)
151-
FROM
152-
(VALUES
153+
INSERT INTO #t
154+
SELECT id,
155+
q,
156+
AI_GENERATE_EMBEDDINGS(q USE MODEL Ada2Embeddings)
157+
FROM (VALUES
153158
(1, N'four legged furry animal'),
154159
(2, N'pink floyd music style')
155-
) S(id, q)
156-
;
157-
158-
SELECT
159-
t.id, s.distance, t.title
160-
FROM
161-
#t AS qv
160+
) AS S(id, q);
161+
SELECT t.id,
162+
s.distance,
163+
t.title
164+
FROM #t AS qv
162165
CROSS APPLY
163166
VECTOR_SEARCH(
164-
TABLE = [dbo].[wikipedia_articles_embeddings] as t,
167+
TABLE = [dbo].[wikipedia_articles_embeddings] AS t,
165168
COLUMN = [content_vector],
166169
SIMILAR_TO = qv.v,
167170
METRIC = 'cosine',
168171
TOP_N = 10
169-
) AS s
170-
WHERE
171-
qv.id = 2
172-
ORDER BY
173-
s.distance
172+
) AS s
173+
WHERE qv.id = 2
174+
ORDER BY s.distance;
174175
```
175176

176177
### Example 3
@@ -236,7 +237,7 @@ ORDER BY s.distance, t.title;
236237

237238
## Related content
238239

239-
- [Overview of vectors in the SQL Database Engine](../../sql-server/ai/vectors.md)
240+
- [Vector search and vector indexes in the SQL Database Engine](../../sql-server/ai/vectors.md)
240241
- [Vector data type](../data-types/vector-data-type.md)
241242
- [CREATE VECTOR INDEX (Transact-SQL)](../statements/create-vector-index-transact-sql.md)
242243
- [Azure SQL Database Vector Search Samples](https://github.com/Azure-Samples/azure-sql-db-vector-search)

0 commit comments

Comments
 (0)