Skip to content

Commit c82dcad

Browse files
Merge pull request #35588 from yorek/dm-vector-search-update-2
removed reference to JSON and updated SQL samples
2 parents 8867d96 + fab8417 commit c82dcad

1 file changed

Lines changed: 23 additions & 21 deletions

File tree

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: VECTOR_SEARCH search for vectors similar to a given query vectors u
44
author: yorek
55
ms.author: damauri
66
ms.reviewer: mikeray, randolphwest
7-
ms.date: 10/14/2025
7+
ms.date: 10/16/2025
88
ms.service: sql
99
ms.subservice: t-sql
1010
ms.topic: reference
@@ -85,7 +85,7 @@ The result set returned by the `VECTOR_SEARCH` function includes:
8585

8686
The distance column is generated by the `VECTOR_SEARCH` function itself, while all other columns come from the table referenced in the `TABLE` argument.
8787

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.
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 built by taking the output of `VECTOR_SEARCH` and merging it with the table data.
8989

9090
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.
9191

@@ -100,9 +100,9 @@ The current preview has the following limitations:
100100
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.
101101

102102
```sql
103-
SELECT s.id,
104-
s.title,
105-
r.distance
103+
SELECT TOP (10) s.id,
104+
s.title,
105+
r.distance
106106
FROM VECTOR_SEARCH(
107107
TABLE = dbo.sessions AS s,
108108
COLUMN = embedding,
@@ -126,10 +126,10 @@ The following example finds the 10 most similar articles to the `Pink Floyd musi
126126

127127
```sql
128128
DECLARE @qv VECTOR(1536) = AI_GENERATE_EMBEDDING(N'Pink Floyd music style' USE MODEL Ada2Embeddings);
129-
SELECT
130-
t.id, s.distance, t.title
131-
FROM
132-
VECTOR_SEARCH(
129+
SELECT TOP (10) s.id,
130+
s.title,
131+
r.distance
132+
FROM VECTOR_SEARCH(
133133
TABLE = [dbo].[wikipedia_articles_embeddings] as t,
134134
COLUMN = [content_vector],
135135
SIMILAR_TO = @qv,
@@ -150,17 +150,19 @@ CREATE TABLE #t
150150
q NVARCHAR (MAX),
151151
v VECTOR(1536)
152152
);
153+
153154
INSERT INTO #t
154155
SELECT id,
155156
q,
156157
AI_GENERATE_EMBEDDINGS(q USE MODEL Ada2Embeddings)
157158
FROM (VALUES
158159
(1, N'four legged furry animal'),
159160
(2, N'pink floyd music style')
160-
) AS S(id, q);
161-
SELECT t.id,
162-
s.distance,
163-
t.title
161+
) S(id, q);
162+
163+
SELECT TOP (10) t.id,
164+
s.distance,
165+
t.title
164166
FROM #t AS qv
165167
CROSS APPLY
166168
VECTOR_SEARCH(
@@ -169,7 +171,7 @@ CROSS APPLY
169171
SIMILAR_TO = qv.v,
170172
METRIC = 'cosine',
171173
TOP_N = 10
172-
) AS s
174+
) AS s
173175
WHERE qv.id = 2
174176
ORDER BY s.distance;
175177
```
@@ -214,23 +216,23 @@ GO
214216

215217
-- Step 3: Create a vector index on the embedding column
216218
CREATE VECTOR INDEX vec_idx ON Articles(embedding)
217-
WITH (metric = 'cosine', type = 'diskann');
219+
WITH (METRIC = 'cosine', TYPE = 'diskann');
218220
GO
219221

220222
-- Step 4: Perform a vector similarity search
221223
DECLARE @qv VECTOR(5) = '[0.3, 0.3, 0.3, 0.3, 0.3]';
222-
SELECT
224+
SELECT TOP(3)
223225
t.id,
224226
t.title,
225227
t.content,
226228
s.distance
227229
FROM
228230
VECTOR_SEARCH(
229-
table = Articles AS t,
230-
column = embedding,
231-
similar_to = @qv,
232-
metric = 'cosine',
233-
top_n = 3
231+
TABLE = Articles AS t,
232+
COLUMN = embedding,
233+
SIMILAR_TO = @qv,
234+
METRIC = 'cosine',
235+
TOP_N = 3
234236
) AS s
235237
ORDER BY s.distance, t.title;
236238
```

0 commit comments

Comments
 (0)