question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

An incorrect query is generated when using a TVP to insert data (`INSERT INTO ... SELECT ... FROM @Table`)

See original GitHub issue

Linq2DB generates incorrect query when a table value parameter is used to insert records in the database.

Steps to reproduce

var recordList = new List<DataModel>();

// ...

static void Test()
{
    var parameter = new DataParameter("table", ToDataRecords(recordList), DataType.Structured, "dbo.MemoryOptimizedTableType");
    
    db.FromSql<DataModel>($"{parameter}")
        .Select(item => new DestTable()
        {
            Id = item.Id,
            Name = item.Name,
        })
        .Into(db.DestTable)
        .Insert();
}

// ...

static IEnumerable<SqlDataRecord> ToDataRecords(IEnumerable<DataModel> list)
{
    // ...
}

Generated query:

INSERT INTO [dbo].[DestTable] DEFAULT VALUES
SELECT
	*
FROM
	@table_1 [item]

Environment details

linq2db version: 3.1.6 Database Server: SQL Server 2019 Database Provider: Microsoft.Data.SqlClient Operating system: Windows Server 2019 .NET Framework: .NET Core 3.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Dec 16, 2020

You have to rewrite your query. This syntax requires providing which fields to insert.

db.FromSql<DataModel>($"{parameter}")
        .Select(item => new DestTable()
        {
            Id = item.Id,
            Name = item.Name,
        })
        .AsCte()
        .Into(db.DestTable)
             .Value(t => t.Id, item => item.Id)
             .Value(t => t.Name, item => iitem.Id)
        .Insert();

Also you can use the following syntax

db.FromSql<DataModel>($"{parameter}")
        .Select(item => new DestTable()
        {
            Id = item.Id,
            Name = item.Name,
        })
        .AsCte()
        .Insert(db.DestTable, item => new DestTable()
        {
            Id = item.Id,
            Name = item.Name,
        });
0reactions
Corey-Mcommented, Jan 21, 2021

This appears to be the same problem I’m having trying to add an INTO clause to a query. The query produces all of the right fields in the right order for the INTO clause, but because I don’t use .Value(...) for each field I get INSERT INTO [target] DEFAULT VALUES... as my generated query.

I have 16 fields in this particular query. Adding each of them via .Value(...) would be tiresome when I’ve already constructed the record.

When .Insert<TSource, TTarget>(...) is called where TSource and TTarget are the same… can we just omit the DEFAULT VALUES please?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inserting data into table from table-valued parameter
1 Answer. Rather than using VALUES with sub-queries, just use SELECT . Always list the columns you are inserting into. Its clearer and...
Read more >
Using Table Valued Parameters (TVP) in SQL Server
The TVP must be declared READONLY. You cannot perform any DML (i.e. INSERT, UPDATE, DELETE) against the TVP; you can only reference it...
Read more >
Queries with Table Value Parameters (TVP) when the Type ...
I was going through some testing against an Azure SQL database and it seems that the TVP type needs to be created on...
Read more >
Inserting Stored Procedure Results into Temporary Table
Learn how to insert the results of a stored procedure into a temporary table in SQL Server. In this data tutorial, we will...
Read more >
t sql - How to insert numerous data into temporary table?
I'm building a program which generates a T-SQL query in following form: DECLARE @In TABLE (Col CHAR(20)) INSERT INTO @In VALUES value1, value2.....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found