Skip to content

Instantly share code, notes, and snippets.

@nkundu
Created May 4, 2017 14:50
Show Gist options
  • Save nkundu/9cf25c8d6e292f7d33cd1d89a1698042 to your computer and use it in GitHub Desktop.
Save nkundu/9cf25c8d6e292f7d33cd1d89a1698042 to your computer and use it in GitHub Desktop.
Stream records to a TVP
/*****
CREATE TYPE [dbo].[BigintCollection] AS TABLE(
[Value] [bigint] NOT NULL,
PRIMARY KEY CLUSTERED
(
[Value] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
ALTER PROCEDURE [dbo].[yourSpName]
-- Add the parameters for the stored procedure here
@inputIds dbo.BigintCollection READONLY
AS
-- STUFF
******/
private static IEnumerable<SqlDataRecord> CreateSqlDataRecords(IEnumerable<long> ids)
{
// http://stackoverflow.com/a/10779567
// the enumerable is not unrolled until just when the data is sent to SQL server
// so we can load millions of records with no memory penalty
SqlMetaData[] metaData = new SqlMetaData[1];
metaData[0] = new SqlMetaData("[Value]", SqlDbType.BigInt);
SqlDataRecord record = new SqlDataRecord(metaData);
foreach (long id in ids)
{
record.SetInt64(0, id);
yield return record;
}
}
public static List<DataType> lookupInfo(IEnumerable<long> inputIds)
{
var results = new List<DataType>();
try
{
using (SqlConnection conn = /* stuff */)
{
SqlCommand cmd = new SqlCommand("yourSpName", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandTimeout = 1000;
var parameter = cmd.Parameters.AddWithValue("@inputIds", CreateSqlDataRecords(inputIds));
parameter.SqlDbType = SqlDbType.Structured;
parameter.TypeName = "dbo.BigintCollection";
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
results.Add(new DataType {
/* stuff */
});
}
}
}
catch (Exception e)
{
/* log */
throw;
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment