Sign in with Microsoft
Sign in or create an account.


Assume that you have a Transact-SQL stored procedure such as the following that takes a table-valued parameter (TVP) that has a constraint as an argument:

create type TestTvpType as table   


  id int primary key, -- UNIQUE will also do the trick

  data int



create procedure TestTvpProc @tvp TestTvpType readonly



  select * from @tvp;



In a scenario where this procedure is called from a SQLCLR stored procedure and the constraint is violated for the argument, you may incorrectly receive a system assert, when you would expect to receive a "constraint violation" error message.

The following is an example of a SQLCLR procedure producing a constraint violation for the stored procedure:


public static void ClrProcCallingTsqlProcWithTvpArgument(out int sum)


       using (SqlConnection connection = new SqlConnection("context connection=true"))


              sum = 0;

              DataTable myDataTable = new DataTable("TestTvpType");

              myDataTable.Columns.Add("id", typeof(Int32));

              myDataTable.Columns.Add("data", typeof(Int32));

              // Populate the TVP so it will trigger a PRIMARY KEY VIOLATION error

              myDataTable.Rows.Add(1, 1000);

              myDataTable.Rows.Add(1, 2000);

              SqlParameter parameter = new SqlParameter();

              parameter.ParameterName = "@tvp";

              parameter.SqlDbType = System.Data.SqlDbType.Structured;

              parameter.Value = myDataTable;

              SqlCommand command = new SqlCommand("TestTvpProc", connection);

              command.CommandType = CommandType.StoredProcedure;



              using (SqlDataReader reader = command.ExecuteReader())


                     while (reader.Read())








This issue is fixed in the following cumulative updates for SQL Server:

Cumulative Update 5 for SQL Server 2016 RTM

Cumulative Update 2 for SQL Server 2016 SP1

Cumulative Update 4 for SQL Server 2014 Service Pack 2

Note This update causes the return of the correct "constraint violation" error message.

Each new cumulative update for SQL Server contains all the hotfixes and security fixes that were included in the previous cumulative update. Check out the latest cumulative updates for SQL Server:

Latest cumulative update for SQL Server 2016

Latest cumulative updates for SQL Server 2014


Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.



Learn about the terminology Microsoft uses to describe software updates.

Need more help?

Expand your skills
Explore Training
Get new features first
Join Microsoft Insiders

Was this information helpful?

What affected your experience?

Thank you for your feedback!