症状
假设你有一个 Transact-sql 存储过程,如下所示,它接受一个具有作为参数的 约束 的表值参数(TVP):
create type TestTvpType as table
(
id int primary key, -- UNIQUE will also do the trick
data int
);
go
create procedure TestTvpProc @tvp TestTvpType readonly
as
begin
select * from @tvp;
end;
go
在从 SQLCLR 存储过程中调用此过程且该参数违反 约束 的情况下,当你希望收到 "限制冲突" 错误消息时,可能会错误地接收系统断言。
以下是为存储过程产生约束冲突的 SQLCLR 过程的示例:
[SqlProcedure]
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;
command.Parameters.Add(parameter);
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
…;
}
}
}
}
解决方案
在 SQL Server 的以下累积更新中修复了此问题:
SQL Server 2014 的累积更新 4 Service Pack 2
注意 此更新会导致返回正确的 "限制冲突" 错误消息。
SQL Server 的每个新的累积更新包含以前的累积更新中包含的所有修补程序和安全修补程序。 查看 SQL Server 的最新累积更新:
状态
Microsoft 已确认这是在“适用范围”部分中列出的 Microsoft 产品存在的问题。
参考
了解 Microsoft 用于描述软件更新的 术语 。