upsert

How to use MERGE or Upsert Sql statement

醉酒当歌 提交于 2019-12-24 07:16:34
问题 How can i use MERGE Sql Statement or UPDATE statement for my below code. I am having a columnName called MachineName , other column values change but MachineName doesnot change. If the Column MachineName changes i need to insert the new values in a secondrow. If not i need to Update the same row. How can i do this. Is it a right approach ? Please help MERGE INTO [devLaserViso].[dbo].[Machine] WITH (HOLDLOCK) USING [devLaserViso].[dbo].[Machine] ON (MachineName = MachineName) WHEN MATCHED THEN

How to implement INSERT ON DUPLICATE KEY UPDATE aka upsert in CakePHP 3?

核能气质少年 提交于 2019-12-24 03:38:06
问题 I am using CakePHP 3 and MySQL. I would like to implement a INSERT on DUPLICATE KEY UPDATE aka upsert query via CakePHP 3 model. Given the following table: +----+----------+-----+ | id | username | age | +----+----------+-----+ | 1 | admin | 33 | | 2 | Timmy | 17 | | 3 | Sally | 23 | +----+----------+-----+ where id is Primary Key and username is unique index When I have the following values awaiting to be upserted: Felicia, 27 Timmy, 71 I expect the following result after the upsert: +----+-

mongo add to nested array if entry does not contain two fields that match

穿精又带淫゛_ 提交于 2019-12-24 03:10:10
问题 I have a mongo document that contains an array called history: { "_id" : ObjectId("575fe85bfe98c1fba0a6e535"), "email" : "email@address", "__v" : 0, "history" : [ { "name" : "Test123", "organisation" : "Rat", "field" : 4, "another": 3 } ] } I want to add fields to each history object or update fields IF the name AND organisation match, however if they don't, I want to add a new object to the array with the queried name and organisation and add/update the other fields to the object when

Replace looping with a single query for INSERT / UPDATE

让人想犯罪 __ 提交于 2019-12-24 00:56:16
问题 I am writing a function in PostgreSQL. It does basically 3 steps: Fetch a record from source table. check the value from the fetched record in target table, if record is found in target table then update all values of target table with fetched record otherwise insert fetched record to target table. Instead of doing this looping, if I write single query for insert/update, will it be faster than above mentioned approach? How can I achieve same result by writing single query instead looping

Updating database from a list of dictionaries

谁说我不能喝 提交于 2019-12-23 22:19:05
问题 In Python, I have a list of dictionaries. The list is called members and each member has a unique id . For example, the list could look like this: members = [{'id':1, 'val1':10, 'val2':11}, {'id':2, 'val1':2, 'val2':34}, {'id':3, 'val1':350, 'val2':9}] I want to update my collection with the list of members, updating and inserting new entries as necessary. Do I need to loop through the members, or is there a faster way? Here's my attempt, which seems to do what I want but takes a while: for m

What is the preferred merge method for SQL Server 2005?

帅比萌擦擦* 提交于 2019-12-23 12:08:41
问题 I have mainly been using the Exists Method for merging a row into a table but I am considering switching to the Row Count Method . Is there any reason not to? Exists Method If Exists(Select * From Table Where ID = @ID) Begin Update Table Set Value = @Value Where ID = @ID End Else Begin Insert Into Table (Value) Values (@Value); End Row Count Method Update Table Set Value = @Value Where ID = @ID If (@@RowCount = 0) Begin Insert Into Table (Value) Values (@Value); End Performance The Row Count

Bulk upsert with SQLAlchemy [duplicate]

旧城冷巷雨未停 提交于 2019-12-23 08:36:12
问题 This question already has answers here : SQLAlchemy - performing a bulk upsert (if exists, update, else insert) in postgresql (1 answer) How to UPSERT (MERGE, INSERT … ON DUPLICATE UPDATE) in PostgreSQL? (6 answers) Closed last year . I am working on bulk upserting lots of data into PostgreSQL with SQLAlchemy 1.1.0b, and I'm running into duplicate key errors. from sqlalchemy import * from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.automap import automap_base import pg engine =

MERGE syntax used to UPSERT or INSERT on duplicate UPDATE

巧了我就是萌 提交于 2019-12-23 08:16:09
问题 So I'm coming from MySQL where I could do INSERT on DUPLICATE UPDATE: INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1; But now I'm using PostgreSQL and there are efforts to add the UPSERT functionality, looks like MERGE might work for what I would like but wanted to see if this is the most optimal syntax. Example Syntax 1, I've also seen this but don't understand how to implement. I haven't tried this yet because I thought MERGE was used for merging data from table1 to

How to do upsert using jongo?

吃可爱长大的小学妹 提交于 2019-12-23 04:06:30
问题 I have a users table/collection and would like to upsert a user - update a user if exists or add a new one if still not exists. Structure below. By "exists", I mean having some external ID. In this case, googleId . How can I do it using Jongo library? Thanks. public class User { public String _id; public String email; public String givenName; public String familyName; public String googleId; } 回答1: Considering having a user with an already loaded googleId (passed google auth), and I'd like to

Upsert error (On Conflict Do Update) pointing to duplicate constrained values

拜拜、爱过 提交于 2019-12-22 05:47:09
问题 I have a problem with ON CONFLICT DO UPDATE in Postgres 9.5 when I try to use more than one source in the FROM statement. Example of working code: INSERT INTO new.bookmonographs (citavi_id, abstract, createdon, edition, title, year) SELECT "ID", "Abstract", "CreatedOn"::timestamp, "Edition", "Title", "Year" FROM old."Reference" WHERE old."Reference"."ReferenceType" = 'Book' AND old."Reference"."Year" IS NOT NULL AND old."Reference"."Title" IS NOT NULL ON CONFLICT (citavi_id) DO UPDATE SET