upsert

Get Id from a conditional INSERT

拜拜、爱过 提交于 2019-11-30 08:31:50
问题 For a table like this one: CREATE TABLE Users( id SERIAL PRIMARY KEY, name TEXT UNIQUE ); What would be the correct one-query insert for the following operation: Given a user name , insert a new record and return the new id . But if the name already exists, just return the id . I am aware of the new syntax within PostgreSQL 9.5 for ON CONFLICT(column) DO UPDATE/NOTHING , but I can't figure out how, if at all, it can help, given that I need the id to be returned. It seems that RETURNING id and

How to correctly do upsert in postgres 9.5

会有一股神秘感。 提交于 2019-11-30 07:45:21
问题 correct syntax of upsert with postgresql 9.5, below query shows column reference "gallery_id" is ambiguous error , why? var dbQuery = `INSERT INTO category_gallery ( category_id, gallery_id, create_date, create_by_user_id ) VALUES ($1, $2, $3, $4) ON CONFLICT (category_id) DO UPDATE SET category_id = $1, last_modified_date = $3, last_modified_by_user_id = $4 WHERE gallery_id = $2`; I tried change WHERE gallery_id = $2; to WHERE category_gallery.gallery_id = $2; then shows error there is no

Upsert Multiple Records with MongoDb

◇◆丶佛笑我妖孽 提交于 2019-11-30 05:37:01
问题 I'm trying to get MongoDB to upsert multiple records with the following query, ultimately using MongoMapper and the Mongo ruby driver. db.foo.update({event_id: { $in: [1,2]}}, {$inc: {visit:1}}, true, true) This works fine if all the records exist, but does not create new records for records that do not exist. The following command has the desired effect from the shell, but is probably not ideal from the ruby driver. [1,2].forEach(function(id) {db.foo.update({event_id: id}, {$inc: {visit:1}},

how to load data faster with talend and sql server

谁说胖子不能爱 提交于 2019-11-30 05:29:57
I use Talend to load data into a sql-server database. It appears that the weakest point of my job is not the dataprocessing, but the effective load in my database, which is not faster than 17 rows/sec. The funny point is that I can launch 5 jobs in the same time, and they'll all load at 17rows/sec . What could explain this slowness and how could I improve the speed? Thanks New informations: The transfer speed between my desktop and the server is about 1MByte My job commits every 10 000 I use sql server 2008 R2 And the schema I use for my jobs is like this: Database INSERT OR UPDATE methods are

How to do an upsert with SqlAlchemy?

允我心安 提交于 2019-11-30 02:38:27
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) name = Column(String(80), unique = True, index = True) template = Column(String(80), unique = True)

How can I use use Entity Framework to do a MERGE when I don't know if the record exists?

隐身守侯 提交于 2019-11-29 23:38:55
In this SO answer about Entity Framework and MERGE, the example for how to code it is this: public void SaveOrUpdate(MyEntity entity) { if (entity.Id == 0) { context.MyEntities.AddObject(entity); } else { context.MyEntities.Attach(entity); context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); } } This assumes that you already know if the entity that you want to upsert exists or not; in this case you check entity.Id . But what if you don't know if the item exists or not? For instance, in my case, I'm importing records from a vendor into my database, and a given record may

UPSERT in SSIS

主宰稳场 提交于 2019-11-29 21:56:42
问题 I am writing an SSIS package to run on SQL Server 2008. How do you do an UPSERT in SSIS? IF KEY NOT EXISTS INSERT ELSE IF DATA CHANGED UPDATE ENDIF ENDIF 回答1: See SQL Server 2008 - Using Merge From SSIS. I've implemented something like this, and it was very easy. Just using the BOL page Inserting, Updating, and Deleting Data using MERGE was enough to get me going. 回答2: I would suggest you to have a look at Mat Stephen's weblog on SQL Server's upsert. SQL 2005 - UPSERT: In nature but not by

PostgreSQL INSERT ON CONFLICT UPDATE (upsert) use all excluded values

旧时模样 提交于 2019-11-29 20:54:52
When you are upserting a row (PostgreSQL >= 9.5), and you want the possible INSERT to be exactly the same as the possible UPDATE, you can write it like this: INSERT INTO tablename (id, username, password, level, email) VALUES (1, 'John', 'qwerty', 5, 'john@mail.com') ON CONFLICT (id) DO UPDATE SET id=EXCLUDED.id, username=EXCLUDED.username, password=EXCLUDED.password, level=EXCLUDED.level,email=EXCLUDED.email Is there a shorter way? To just say: use all the EXCLUDE values. In SQLite I used to do : INSERT OR REPLACE INTO tablename (id, user, password, level, email) VALUES (1, 'John', 'qwerty',

Meteor upsert equivalent

独自空忆成欢 提交于 2019-11-29 15:32:22
问题 How soon will the upsert command be implemented in Meteor? And, what is the best way to do the same thing in the mean time? Something like this is what I'm doing at the moment: if typeof ( item = Items.findOne({title:'Foo'}) ) == 'undefined' item = Items.insert({title:'Foo'}) else Items.update(item._id, {$set: {title:'Foo'}}) # do something with item 回答1: How soon will the upsert command be implemented in Meteor? UPDATE: @Thomas4019 points out that upsert is now supported: v0.6.6 "Add upsert

Partial Index not used in ON CONFLICT clause while performing an upsert in Postgresql

爷,独闯天下 提交于 2019-11-29 14:19:55
I have the following Entity Attribute value table : CREATE TABLE key_value_pair ( id serial NOT NULL PRIMARY KEY, key varchar(255) NOT NULL, value varchar(255), is_active boolean ); CREATE UNIQUE INDEX key_value_pair_key_if_is_active_true_unique ON key_value_pair (key) WHERE is_active = true; Sample entries in this table are : id | key | value | is_active ----+-------------+-------+----------- 1 | temperature | 2 | f 2 | temperature | 12 | f 3 | temperature | 15 | f 4 | temperature | 19 | f 5 | temperature | 23 | t (5 rows) Thus, at any point in time, for any given key, only 1 true is_active