-
Notifications
You must be signed in to change notification settings - Fork 20
TVP support
sdrapkin edited this page May 12, 2017
·
5 revisions
TinyORM has full support for Table-Valued Parameters (TVP), supported by User-Defined Table Types (UDTT).
Creating a single-column UDTT:
CREATE TYPE GuidTable AS TABLE
(
Id UNIQUEIDENTIFIER PRIMARY KEY CLUSTERED
)
Once you have a suitable UDTT defined within a SQL Server database, you can use TinyORM to send arrays or lists of values as TVPs, which will be accessible as table variables:
var db = DbContext.Create(connString);
var data = Enumerable.Range(0,1000).Select(i=> Guid.NewGuid()).ToArray(); // let's generate some data
var tvp = data.AsTVP("GuidTable"); // using "GuidTable" UDTT created above
var result = await db.QueryAsync("select * from @tvp", new { tvp });
result.Select(row => row.Id).Dump(); // displays 1000 guids
Complex multi-column TVPs can be sent via a custom-built DataTable
instance.
Memory-optimized UDTTs are supported as well.
Copyright (c) 2016-2022 Stan Drapkin