There are many questions regarding this error, but none seem related.
The original exception we get
Der eingehende Tabular Data Stream (TDS) für das RPC-Protokoll (Remote Procedure Call) ist nicht richtig.
0-Tabellenwertparameter (""), Zeile 0, Spalte 0: Der 0xE7-Datentyp hat eine ungültige Daten- oder Metadatenlänge.
My attempt at translating it
The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect
0-TableValuedParameter (""),Row 0, Column 0: The 0xE7-Datatype has an invalid Data- or Metadatalength.
The Version of SqlServer
Microsoft SQL Server 2016 (SP1) (KB3182545) - 13.0.4001.0 (X64) Oct 28 2016 18:17:30 Copyright (c) Microsoft Corporation Standard Edition (64-bit) on Windows Server 2016 Standard 6.3 (Build 14393: ) (Hypervisor)
What is causing this:
We use user defined types to pass table valued parameters. The error is reproducable passing a DataTable with a single row and column containing a String.Empty
as a prameter for a type like the following:
CREATE TYPE dbo.para_table_varchar AS TABLE (value nvarchar(10) COLLATE Latin1_General_ci_ai NOT NULL);
The C# Code (.net 4.6.1)
DataTable table = new DataTable();
DataColumn column = table.Columns.Add("value", typeof(string));
column.MaxLength = 0;
table.Rows.Add(string.Empty);
SqlParameter para = _cmd.Parameters.Add("@ptable", SqlDbType.Structured, 0);
para.Value = table;
para.TypeName ="dbo.para_table_varchar";
The Command looks like this
SELECT * FROM someTable WHERE someColumn IN (SELECT value FROM @ptable)
This only happens on the mentioned SQLServer 2016. About 20 other clients do not have this issue.
While Microsoft covers this issue here, the suggested workarround (setting Size to -1) doesn't seem to work.