Skip to content

Conversion failed when using User Defined Table Types #424

Open

Description

@daniellittledev

Issue Summary

When using a User Defined Table Type it appears to write the value to the wrong column, resulting in a database conversion error. Using the SQL profiler I can see this error occurs before executing the SQL and only when the @parameter has one or more items. If it is empty or removed the command executes successfully.

Error

System.Data.SqlClient.SqlException: 'Conversion failed when converting the nvarchar value 'TestName' to data type int.
The statement has been terminated.'

To Reproduce

Possibly share SQL Schema, snippets of data, and how you call it in F#.

Sample to reproduce the behavior:

Table and Table Type

Create Table [TestTable] (
	[C1] UniqueIdentifier Not Null,
	[C2] UniqueIdentifier Not Null,
	[C3] UniqueIdentifier Not Null,

	[C4] NVarChar(100) Not Null,
	[C5] NVarChar(100) Not Null,
	[C6] NVarChar(100) Null,
	[C7] NVarChar(1000) Not Null,

	[C8] Int Not Null,

	[C9] VarChar(10) Not Null,
	[C10] UniqueIdentifier Not Null,
	[C11] UniqueIdentifier Not Null,

	Constraint [PK_Id] Primary Key Clustered([C1], [C2], [C3])
);
Create Type [dbo].[TestTableItem] AS Table(
	[C3] UniqueIdentifier Not Null,
	[C5] NVarChar(100) Not Null,
	[C6] NVarChar(100) Null,
	[C7] NVarChar(1000) Not Null,
	[C8] Int Not Null,
	[C9] VarChar(10) Not Null,
	[C10] UniqueIdentifier Not Null
);

SqlCommandProvider

type AddSqlCommand = SqlCommandProvider<"
    Insert [TestTable]
    (
        [C1],
        [C2],
        [C11],
        [C4],
        [C2],
        [C5],
        [C6],
        [C7],
        [C8],
        [C9],
        [C10]
    )
    Select
        @C1,
        @C2,
        @C11,
        @C4,
        x.[C3],
        x.[C5],
        x.[C6],
        x.[C7],
        x.[C8],
        x.[C9],
        x.[C10]
    From @C12 x;
    " , staticConnectionString, TableVarMapping = "@C12=TestTableItem">

Usage

use cmd = new AddSqlCommand(connection, transaction = transaction)

let items =
    [
        AddSqlCommand.TestTableItem(
            C3 = Guid.NewGuid(),
            C5 = "TestName",
            C6 = "C6",
            C7 = "C7",
            C8 = 1,
            C9 = "C9",
            C10 = Guid.NewGuid()
        )
    ]

return!
    cmd.AsyncExecute(
        C1 = Guid.NewGuid(),
        C2 = Guid.NewGuid(),
        C4 = "C4 Name",
        C11 = Guid.NewGuid(),
        C12 = items
    )

Expected behavior

The query should successfully insert a new row.

What you can do

  • I am willing to contribute a PR with a unit test showcasing the issue
  • I am willing to test the bug fix before next release

Activity

  1. smoothdeveloper commented on Dec 3, 2022

    @smoothdeveloper
    Collaborator

    @daniellittledev I've tried to reproduce the issue in #425 but the added test doesn't throw a SqlException, do you mind trying it out and adjust the test accordingly to reproduce your issue?

  2. github-actions commented on Mar 13, 2026

    @github-actions
    Contributor

    🤖 This is an automated response from Repo Assist.

    Looking at the reproduction case, there appears to be a duplicate column name in the INSERT target list that might be causing the conversion error. In the sample SQL:

    Insert [TestTable](
        [C1],
        [C2],
        [C11],
        [C4],
        [C2],   ← duplicate! [C3] was likely intended here
        [C5],
        ...

    Column [C2] appears twice at positions 2 and 5, and [C3] is missing entirely. When SQL Server evaluates this with the UDTT values from @C12, position 5 of the VALUES (x.[C5], which is NVarChar(100)) is mapped against the wrong target column due to the duplicate. The resulting positional mismatch can cause the type conversion error you're seeing.

    To investigate: replace the second [C2] in your INSERT column list with [C3] — this would align the UDTT column values with the correct target columns.

    Separately, on the library side: I traced through how SqlMetaData entries are generated for UDTT columns (in DesignTime.fs CreateUDTT) and column ordering there looks correct — columns are fetched from sys.columns ordered by column_id, and the constructor parameter order matches the SqlMetaData array order. So I don't believe the library is reordering columns.

    @daniellittledev — if the fix above resolves the issue, feel free to close. If you're seeing the conversion error even with a correct (non-duplicate) column list, please share the updated repro so the maintainer can investigate further with the test in PR #425.

    Generated by Repo Assist · ◷

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/repo-assist.md@346204513ecfa08b81566450d7d599556807389f
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions