Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table-Valued Parameter without using a stored procedure

It seems all I can find online are examples of table-valued parameters that require using a stored procedure. I recall doing this in the past without the stored procedure. Is that possible?

This code keeps throwing an error about the type not being specified.

SqlCommand cmd = new SqlCommand(@"
        UPDATE t1
        SET t1.ScheduledStartUTC = t2.ScheduledStartUTC
        FROM ScheduleTickets AS t1
            INNER JOIN @SetScheduledStart AS t2 ON t1.ScheduleId = t2.ScheduleId AND t1.PatchSessionId  = t2.PatchSessionId 
    ", c);
cmd.Parameters.Add("@SetScheduledStart", SqlDbType.Structured).Value = SetScheduleTicketsDateDT;
cmd.ExecuteNonQuery();
like image 703
Connie DeCinko CS Avatar asked Mar 19 '18 14:03

Connie DeCinko CS


1 Answers

Here you can find how to use it without stored procedure: Passing a Table-Valued Parameter to a Parameterized SQL Statement

Basically, it requires you to:

  1. CREATE TYPE dbo.tvpUpdateScheduledStart AS TABLE (ScheduleId int, PatchSessionId int) on the server beforehand.
  2. Specify this type in TypeName property of a SqlParameter.
like image 113
astef Avatar answered Oct 06 '22 01:10

astef