You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/t-sql/functions/vector-search-transact-sql.md
+23-21Lines changed: 23 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ description: VECTOR_SEARCH search for vectors similar to a given query vectors u
4
4
author: yorek
5
5
ms.author: damauri
6
6
ms.reviewer: mikeray, randolphwest
7
-
ms.date: 10/14/2025
7
+
ms.date: 10/16/2025
8
8
ms.service: sql
9
9
ms.subservice: t-sql
10
10
ms.topic: reference
@@ -85,7 +85,7 @@ The result set returned by the `VECTOR_SEARCH` function includes:
85
85
86
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
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.
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.
89
89
90
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.
91
91
@@ -100,9 +100,9 @@ The current preview has the following limitations:
100
100
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.
101
101
102
102
```sql
103
-
SELECTs.id,
104
-
s.title,
105
-
r.distance
103
+
SELECTTOP (10) s.id,
104
+
s.title,
105
+
r.distance
106
106
FROM VECTOR_SEARCH(
107
107
TABLE =dbo.sessionsAS s,
108
108
COLUMN = embedding,
@@ -126,10 +126,10 @@ The following example finds the 10 most similar articles to the `Pink Floyd musi
126
126
127
127
```sql
128
128
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(
133
133
TABLE = [dbo].[wikipedia_articles_embeddings] as t,
134
134
COLUMN = [content_vector],
135
135
SIMILAR_TO = @qv,
@@ -150,17 +150,19 @@ CREATE TABLE #t
150
150
q NVARCHAR (MAX),
151
151
v VECTOR(1536)
152
152
);
153
+
153
154
INSERT INTO#t
154
155
SELECT id,
155
156
q,
156
157
AI_GENERATE_EMBEDDINGS(q USE MODEL Ada2Embeddings)
157
158
FROM (VALUES
158
159
(1, N'four legged furry animal'),
159
160
(2, N'pink floyd music style')
160
-
) AS S(id, q);
161
-
SELECTt.id,
162
-
s.distance,
163
-
t.title
161
+
) S(id, q);
162
+
163
+
SELECT TOP (10) t.id,
164
+
s.distance,
165
+
t.title
164
166
FROM#t AS qv
165
167
CROSS APPLY
166
168
VECTOR_SEARCH(
@@ -169,7 +171,7 @@ CROSS APPLY
169
171
SIMILAR_TO =qv.v,
170
172
METRIC ='cosine',
171
173
TOP_N =10
172
-
) AS s
174
+
) AS s
173
175
WHEREqv.id=2
174
176
ORDER BYs.distance;
175
177
```
@@ -214,23 +216,23 @@ GO
214
216
215
217
-- Step 3: Create a vector index on the embedding column
216
218
CREATE VECTOR INDEX vec_idx ON Articles(embedding)
0 commit comments