An incorrect query is generated when using a TVP to insert data (`INSERT INTO ... SELECT ... FROM @Table`)
See original GitHub issueLinq2DB 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:
- Created 3 years ago
- Comments:11 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You have to rewrite your query. This syntax requires providing which fields to insert.
Also you can use the following syntax
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 theINTO
clause, but because I don’t use.Value(...)
for each field I getINSERT 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 whereTSource
andTTarget
are the same… can we just omit theDEFAULT VALUES
please?