@@ -4,7 +4,7 @@ description: The vector data type stores vector data optimized for machine learn
4
4
author : WilliamDAssafMSFT
5
5
ms.author : wiassaf
6
6
ms.reviewer : damauri, pookam
7
- ms.date : 05/01 /2025
7
+ ms.date : 07/25 /2025
8
8
ms.update-cycle : 180-days
9
9
ms.service : sql
10
10
ms.subservice : t-sql
@@ -140,37 +140,107 @@ You can start using the new **vector** type right away. The following examples s
140
140
> Requires ** Microsoft.Data.SqlClient 6.1.0** or later for native vector support.
141
141
142
142
``` csharp
143
- static void InsertNonNullVal (SqlConnection conn )
144
- {
145
- Console .WriteLine (" Inserting non-null value with SqlDbType" );
146
143
147
- using SqlCommand command = new SqlCommand (" INSERT INTO dbo.vectors VALUES (@Id, @VectorData)" , conn );
144
+ using Microsoft .Data ;
145
+ using Microsoft .Data .SqlClient ;
146
+ using Microsoft .Data .SqlTypes ;
148
147
149
- command .Parameters .AddWithValue (" @Id" , 1 );
148
+ namespace VectorSampleApp
149
+ {
150
+ class Program
151
+ {
152
+ // Set your environment variable or fallback to local server
153
+ private static readonly string connectionString =
154
+ Environment .GetEnvironmentVariable (" CONNECTION_STR" )
155
+ ?? " Server=tcp:localhost,1433;Database=Demo2;Integrated Security=True;TrustServerCertificate=True" ;
150
156
151
- var vectorParameter = new SqlParameter (" @VectorData" , Microsoft .Data .SqlDbTypeExtensions .Vector );
152
- vectorParameter .Value = new Microsoft .Data .SqlTypes .SqlVectorFloat32 (new float [] { 3 . 14159 f , 1 . 61803 f , 1 . 41421 f });
153
- command .Parameters .Add (vectorParameter );
157
+ private const int VectorDimensions = 3 ;
158
+ private const string TableName = " dbo.Vectors" ;
154
159
155
- command .ExecuteNonQuery ();
156
- }
160
+ static void Main ()
161
+ {
162
+ using var connection = new SqlConnection (connectionString );
163
+ connection .Open ();
164
+ SetupTables (connection , TableName , VectorDimensions );
165
+ InsertVectorData (connection , TableName );
166
+ ReadVectorData (connection , TableName );
167
+ }
157
168
158
- static void ReadWithGetValue (SqlConnection connection )
159
- {
160
- Console .WriteLine (" Reading values using GetValue method:" );
169
+ private static void SetupTables (SqlConnection connection , string tableName , int vectorDimensionCount )
170
+ {
171
+ using var command = connection .CreateCommand ();
172
+
173
+ command .CommandText = $@"
174
+ IF OBJECT_ID('{tableName }', 'U') IS NOT NULL DROP TABLE {tableName };
175
+ IF OBJECT_ID('{tableName }Copy', 'U') IS NOT NULL DROP TABLE {tableName }Copy;" ;
176
+ command .ExecuteNonQuery ();
177
+
178
+ command .CommandText = $@"
179
+ CREATE TABLE {tableName } (
180
+ Id INT IDENTITY(1,1) PRIMARY KEY,
181
+ VectorData VECTOR({vectorDimensionCount })
182
+ );
183
+
184
+ CREATE TABLE {tableName }Copy (
185
+ Id INT IDENTITY(1,1) PRIMARY KEY,
186
+ VectorData VECTOR({vectorDimensionCount })
187
+ );" ;
188
+ command .ExecuteNonQuery ();
189
+ }
161
190
162
- using (SqlCommand commandSourceData = new SqlCommand (" SELECT v FROM dbo.vectors;" , connection ))
163
- using (SqlDataReader reader = commandSourceData .ExecuteReader ())
164
- while (reader .Read ())
165
- {
166
- var vector = reader .GetValue (0 );
167
- if (vector != null && vector != DBNull .Value )
191
+ private static void InsertVectorData (SqlConnection connection , string tableName )
192
+ {
193
+ using var command = new SqlCommand ($" INSERT INTO {tableName } (VectorData) VALUES (@VectorData)" , connection );
194
+ var param = command .Parameters .Add (" @VectorData" , SqlDbTypeExtensions .Vector );
195
+
196
+ // Insert null using DBNull.Value
197
+ param .Value = DBNull .Value ;
198
+ command .ExecuteNonQuery ();
199
+
200
+ // Insert non-null vector
201
+ param .Value = new SqlVector <float >(new float [] { 3 . 14159 f , 1 . 61803 f , 1 . 41421 f });
202
+ command .ExecuteNonQuery ();
203
+
204
+ // Insert typed null vector
205
+ param .Value = SqlVector <float >.CreateNull (VectorDimensions );
206
+ command .ExecuteNonQuery ();
207
+
208
+ // Prepare once and reuse for loop
209
+ command .Prepare ();
210
+ for (int i = 0 ; i < 10 ; i ++ )
211
+ {
212
+ param .Value = new SqlVector <float >(new float []
213
+ {
214
+ i + 0 . 1 f ,
215
+ i + 0 . 2 f ,
216
+ i + 0 . 3 f
217
+ });
218
+ command .ExecuteNonQuery ();
219
+ }
220
+ }
221
+
222
+ private static void ReadVectorData (SqlConnection connection , string tableName )
168
223
{
169
- Console .WriteLine (" Type: " + vector .GetType () + " Element Count: " + ((SqlVectorFloat32 )vector ).Length );
224
+ using var command = new SqlCommand ($" SELECT VectorData FROM {tableName }" , connection );
225
+ using var reader = command .ExecuteReader ();
226
+
227
+ while (reader .Read ())
228
+ {
229
+ var sqlVector = reader .GetSqlVector <float >(0 );
170
230
171
- var values = ((SqlVectorFloat32 )vector ).Values ;
172
- Console .WriteLine (" Values: " + string .Join (" , " , values ));
173
- }
231
+ Console .WriteLine ($" Type: {sqlVector .GetType ()}, IsNull: {sqlVector .IsNull }, Length: {sqlVector .Length }" );
232
+
233
+ if (! sqlVector .IsNull )
234
+ {
235
+ float [] values = sqlVector .Memory .ToArray ();
236
+ Console .WriteLine (" VectorData: " + string .Join (" , " , values ));
237
+ }
238
+ else
239
+ {
240
+ Console .WriteLine (" VectorData: NULL" );
241
+ }
242
+ }
243
+ }
174
244
}
175
245
}
176
246
```
@@ -208,9 +278,9 @@ class Program
208
278
### [ JDBC] ( #tab/jdbc )
209
279
210
280
> [ !IMPORTANT]
211
- > Requires ** Microsoft JDBC Driver for SQL Server 13.1.0 Preview or later** for native vector type support.
281
+ > Requires ** Microsoft JDBC Driver for SQL Server 13.1.0 or later versions ** for native vector type support.
212
282
213
- Example of Insertion of ** vector** data into table
283
+ Example of insertion of ** vector** data into table:
214
284
215
285
``` java
216
286
@Test
@@ -245,7 +315,43 @@ public void getVectorData() throws SQLException {
245
315
}
246
316
}
247
317
```
248
-
318
+
319
+ Example of selecting ** vector** data from a table:
320
+
321
+ ``` java
322
+ @Test
323
+ public void getVectorData() throws SQLException {
324
+ String query = " SELECT v FROM " + AbstractSQLGenerator . escapeIdentifier(tableName);
325
+ try (PreparedStatement stmt = connection. prepareStatement(query)) {
326
+ try (ResultSet rs = stmt. executeQuery()) {
327
+ assertTrue(rs. next(), " No result found for inserted vector." );
328
+ ResultSetMetaData meta = rs. getMetaData();
329
+ int columnCount = meta. getColumnCount();
330
+
331
+ while (rs. next()) {
332
+ for (int i = 1 ; i <= columnCount; i++ ) {
333
+ String columnName = meta. getColumnName(i);
334
+ int columnType = meta. getColumnType(i); // from java.sql.Types
335
+
336
+ Object value = null ;
337
+ switch (columnType) {
338
+ case Types . VARCHAR:
339
+ case Types . NVARCHAR:
340
+ value = rs. getString(i);
341
+ break ;
342
+ case microsoft.sql. Types . VECTOR:
343
+ value = rs. getObject(i, microsoft.sql. Vector . class);
344
+ }
345
+
346
+ System . out. println(columnName + " = " + value + " (type: " + columnType + " )" );
347
+ }
348
+ System . out. println(" ---" );
349
+ }
350
+ }
351
+ }
352
+ }
353
+ ```
354
+
249
355
### [ Python (mssql-python)] ( #tab/python )
250
356
251
357
With Python, applications can write and read vector data using ` json.loads ` and ` json.dumps ` :
@@ -362,4 +468,4 @@ The **vector** type has the following limitations:
362
468
363
469
- [ Overview of vectors in the SQL Database Engine] ( ../../relational-databases/vectors/vectors-sql-server.md )
364
470
- [ Intelligent applications] ( /azure/azure-sql/database/ai-artificial-intelligence-intelligent-applications?view=azuresql&preserve-view=true#vector-search )
365
- - [ Vector Functions] ( ../functions/vector-functions-transact-sql.md )
471
+ - [ Vector Functions] ( ../functions/vector-functions-transact-sql.md )
0 commit comments