Batch update via temp table [Question]
See original GitHub issueHey Everybody, I’m trying to implement batch upsert logic for Postgres. As Postgres doesn’t support Merge
statement yet (https://github.com/linq2db/linq2db/issues/892), I have to stick with temporary table to hold updated records.
The question is how can I write the following query via linq expressions to update existing records in the original table in single query?
UPDATE origin_table AS record
SET column1 = temp_record.column1,
column2 = temp_record.column2,
column3 = temp_record.column3
FROM temp_table AS temp_record
WHERE record.id = temp_record.id
Is it possible with linq2db ? Currently I’m executing this as raw query like:
var result = db.Query<dynamic>(
@"UPDATE customers AS record
SET code = temp_record.code,
tenant_id = temp_record.tenant_id
FROM temp_customers AS temp_record
WHERE record.id = temp_record.id"
).ToList();
Is there better way to do this ?
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (7 by maintainers)
Top Results From Across the Web
SQL Batch Update with a temp table join
I want to do the update via a join as I have other rows to update using information from a variety of sources...
Read more >How to update SQL Server table from a Temp table
In . NET code I would simply iterate over the temp table, grab the database name id value, go get the database name...
Read more >“Bulk” update in mysql with the use of temporary table
For new records, using bulk insertion is pretty easy. Updating existing rows is quite a different story as there's not yet an official...
Read more >How to Batch Updates A Few Thousand Rows at a Time
Setting up the problem. I'll start with the Stack Overflow database (any version will work) and make a copy of the Users table...
Read more >CTE vs. temp table for batch deletes - sql server
Both the CTE and temp table are available using paste the plan (links above). There are FKs to two other tables with cascading...
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
This sample also should work, just ensure that first recordset in query is table that needs to be updated:
Using temp table in query string is not working in SQL Server too. Is there a way we can access temp table in raw sql? Getting object not found error.
using (var tempTable = db.CreateTempTable(tempTableName, customers)) { var recordsToUpdate = from c in db.GetTable<customer>() from t in tempTable.InnerJoin(t => t.id == c.id) select new {c, t};
}