Skip to content

Commit 407a012

Browse files
authored
Merge pull request #34797 from Pookam90/patch-6
Update vector-data-type.md
2 parents 069687f + df09726 commit 407a012

File tree

1 file changed

+134
-28
lines changed

1 file changed

+134
-28
lines changed

docs/t-sql/data-types/vector-data-type.md

Lines changed: 134 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: The vector data type stores vector data optimized for machine learn
44
author: WilliamDAssafMSFT
55
ms.author: wiassaf
66
ms.reviewer: damauri, pookam
7-
ms.date: 05/01/2025
7+
ms.date: 07/25/2025
88
ms.update-cycle: 180-days
99
ms.service: sql
1010
ms.subservice: t-sql
@@ -140,37 +140,107 @@ You can start using the new **vector** type right away. The following examples s
140140
> Requires **Microsoft.Data.SqlClient 6.1.0** or later for native vector support.
141141
142142
```csharp
143-
static void InsertNonNullVal(SqlConnection conn)
144-
{
145-
Console.WriteLine("Inserting non-null value with SqlDbType");
146143

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;
148147

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";
150156

151-
var vectorParameter = new SqlParameter("@VectorData", Microsoft.Data.SqlDbTypeExtensions.Vector);
152-
vectorParameter.Value = new Microsoft.Data.SqlTypes.SqlVectorFloat32(new float[] { 3.14159f, 1.61803f, 1.41421f });
153-
command.Parameters.Add(vectorParameter);
157+
private const int VectorDimensions = 3;
158+
private const string TableName = "dbo.Vectors";
154159

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+
}
157168

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+
}
161190

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.14159f, 1.61803f, 1.41421f });
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.1f,
215+
i + 0.2f,
216+
i + 0.3f
217+
});
218+
command.ExecuteNonQuery();
219+
}
220+
}
221+
222+
private static void ReadVectorData(SqlConnection connection, string tableName)
168223
{
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);
170230

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+
}
174244
}
175245
}
176246
```
@@ -208,9 +278,9 @@ class Program
208278
### [JDBC](#tab/jdbc)
209279

210280
> [!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.
212282
213-
Example of Insertion of **vector** data into table
283+
Example of insertion of **vector** data into table:
214284

215285
```java
216286
@Test
@@ -245,7 +315,43 @@ public void getVectorData() throws SQLException {
245315
}
246316
}
247317
```
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+
249355
### [Python (mssql-python)](#tab/python)
250356

251357
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:
362468

363469
- [Overview of vectors in the SQL Database Engine](../../relational-databases/vectors/vectors-sql-server.md)
364470
- [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

Comments
 (0)