upsert

Performing Insert OR Update (upsert) on sql server compact edition

徘徊边缘 提交于 2019-11-27 22:42:06
问题 I have c# project that is using sqlserver compact edition and entity framework for data access. I have the need to insert or update a large amount of rows, 5000+ or more to the db, so if the key exists update the record if not insert it. I can not find a way to do this with compact edition and EF with out horrible performance, ie taking 2 mins plus on a core i7 computer. I have tried searching for the record to see if it exists then inserting if not or update if it does, the search is the

What is USING in SQL Server 2008 MERGE syntax?

喜你入骨 提交于 2019-11-27 20:58:09
问题 Jacob asked the perfect question: give me the MERGE syntax. Every answer out there immediately jumps to the most complicated case they can think of; obscuring the syntax with extraneous confusion. Marc gave an answer: MERGE member_topic AS target USING someOtherTable AS source ON target.mt_member = source.mt_member AND source.mt_member = 0 AND source.mt_topic = 110 WHEN MATCHED THEN UPDATE SET mt_notes = 'test' WHEN NOT MATCHED THEN INSERT (mt_member, mt_topic, mt_notes) VALUES (0, 110, 'test

Generate DEFAULT values in a CTE UPSERT using PostgreSQL 9.3

▼魔方 西西 提交于 2019-11-27 15:09:52
I'm finding that using writable CTEs to emulate an upsert in PostgreSQL to be quite an elegant solution until we get actual upsert/merge in Postgres. (see: https://stackoverflow.com/a/8702291/558819 ) However, there is one problem: how can I insert the default value? Using NULL won't help of course as NULL gets explicitly inserted as NULL , unlike for example with MySQL. An example: WITH new_values (id, playlist, item, group_name, duration, sort, legacy) AS ( VALUES (651, 21, 30012, 'a', 30, 1, FALSE) , (NULL::int, 21, 1, 'b', 34, 2, NULL::boolean) , (668, 21, 30012, 'c', 30, 3, FALSE) , (7428

PostgreSQL Upsert differentiate inserted and updated rows using system columns XMIN, XMAX and others

北战南征 提交于 2019-11-27 14:03:36
Disclaimer: theoretical question. Several questions here was asked about how to differentiate inserted and updated rows in the PostgreSQL upsert statement. Here is a simple example: nd@postgres=# create table t(i int primary key, x int); nd@postgres=# insert into t values(1,1); nd@postgres=# insert into t values(1,11),(2,22) on conflict(i) do update set x = excluded.i*11 returning *, xmin, xmax; ╔═══╤════╤══════╤══════╗ ║ i │ x │ xmin │ xmax ║ ╠═══╪════╪══════╪══════╣ ║ 1 │ 11 │ 7696 │ 7696 ║ ║ 2 │ 22 │ 7696 │ 0 ║ ╚═══╧════╧══════╧══════╝ So, xmax > 0 (or xmax = xmin ) - row was updated; xmax

get mongodb _id object after upsert with php

梦想与她 提交于 2019-11-27 13:25:54
问题 is it possible to get the new/updated _id after the query? example code: $key = array( 'something' => 'unique' ); $data = array( '$inc' => array( 'someint' => 1 ) ); $mongodb->db->collection->update( $key, $data, array( 'upsert' => true ) ); $key is not holding the new/old _id object and i assume that $data will not either because its just an instruction. 回答1: Yes -- It is possible using a single query. MongoDB includes a findAndModify command that can atomically modify a document and return

Fast or Bulk Upsert in pymongo

安稳与你 提交于 2019-11-27 11:47:47
How can I do a bulk upsert in pymongo? I want to Update a bunch of entries and doing them one at a time is very slow. The answer to an almost identical question is here: Bulk update/upsert in MongoDB? The accepted answer doesn't actually answer the question. It simply gives a link to the mongo CLI for doing import/exports. I would also be open to someone explaining why doing a bulk upsert is no possible / no a best practice, but please explain what the preferred solution to this sort of problem is. Kevin J. Rice MongoDB 2.6+ has support for bulk operations. This includes bulk inserts, upserts,

How to include excluded rows in RETURNING from INSERT … ON CONFLICT

孤人 提交于 2019-11-27 08:25:00
I've got this table (generated by Django): CREATE TABLE feeds_person ( id serial PRIMARY KEY, created timestamp with time zone NOT NULL, modified timestamp with time zone NOT NULL, name character varying(4000) NOT NULL, url character varying(1000) NOT NULL, email character varying(254) NOT NULL, CONSTRAINT feeds_person_name_ad8c7469_uniq UNIQUE (name, url, email) ); I'm trying to bulk insert a lot of data using INSERT with an ON CONFLICT clause. The wrinkle is that I need to get the id back for all of the rows, whether they're already existing or not. In other cases, I would do something like:

What happens with duplicates when inserting multiple rows?

霸气de小男生 提交于 2019-11-27 07:03:21
问题 I am running a python script that inserts a large amount of data into a Postgres database, I use a single query to perform multiple row inserts: INSERT INTO table (col1,col2) VALUES ('v1','v2'),('v3','v4') ... etc I was wondering what would happen if it hits a duplicate key for the insert. Will it stop the entire query and throw an exception? Or will it merely ignore the insert of that specific row and move on? 回答1: The INSERT will just insert all rows and nothing special will happen, unless

SQLite “INSERT OR REPLACE INTO” vs. “UPDATE … WHERE”

一个人想着一个人 提交于 2019-11-27 06:35:44
I've never seen the syntax INSERT OR REPLACE INTO names (id, name) VALUES (1, "John") used in SQL before, and I was wondering why it's better than UPDATE names SET name = "John" WHERE id = 1 . Is there any good reason to use one over the other. Is this syntax specific to SQLite? Adriaan Stander UPDATE will not do anything if the row does not exist. Where as the INSERT OR REPLACE would insert if the row does not exist, or replace the values if it does. cybergen I'm currently working on such a statement and figured out another fact to notice: INSERT OR REPLACE will replace any values not

How to do an upsert with SqlAlchemy?

故事扮演 提交于 2019-11-27 05:42:21
问题 I have a record that I want to exist in the database if it is not there, and if it is there already (primary key exists) I want the fields to be updated to the current state. This is often called an upsert. The following incomplete code snippet demonstrates what will work, but it seems excessively clunky (especially if there were a lot more columns). What is the better/best way? Base = declarative_base() class Template(Base): __tablename__ = 'templates' id = Column(Integer, primary_key = True