6

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();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Connie DeCinko CS
  • 151
  • 1
  • 2
  • 14
  • 8
    That's because you're also required to set the `TypeName` property if you're using `SqlDbType.Structured` for TVPs. (And you cannot get around creating this type first, if that's what you're going for.) – Jeroen Mostert Mar 19 '18 at 14:56
  • 2
    Ok, I found how to specify the TypeName: cmd.Parameters["@SetScheduledStart"].TypeName = "dbo.tvpUpdateScheduledStart"; – Connie DeCinko CS Mar 19 '18 at 15:43

1 Answers1

1

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.
astef
  • 8,575
  • 4
  • 56
  • 95