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.

Batch update via temp table [Question]

See original GitHub issue

Hey 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:closed
  • Created 5 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Jul 28, 2018

This sample also should work, just ensure that first recordset in query is table that needs to be updated:

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};

	recordsToUpdate
		.Set(v => v.c.code,      v => v.t.code)
		.Set(v => v.c.tenant_id, v => v.t.tenant_id)
		.Update();
}
0reactions
anirujogicommented, Apr 16, 2021

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};

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();

}

Read more comments on GitHub >

github_iconTop 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 >

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