upsert

MySQL behavior of ON DUPLICATE KEY UPDATE for multiple UNIQUE fields

亡梦爱人 提交于 2019-11-26 20:36:09
问题 From MySQL 4.1.0 onwards, it is possible to add ON DUPLICATE KEY UPDATE statement to specify behavior when values inserted (with INSERT or SET or VALUES ) are already in destination table w.r.t. PRIMARY KEY or some UNIQUE field. If value for PRIMARY KEY or some UNIQUE field are already in table, INSERT is replaced by an UPDATE . How does ON DUPLICATE KEY UPDATE behave in case there are multiple UNIQUE fields in my table ? Can I have one update only, if either UNIQUE field is matched ? Can I

Insert Update stored proc on SQL Server

独自空忆成欢 提交于 2019-11-26 19:38:43
I've written a stored proc that will do an update if a record exists, otherwise it will do an insert. It looks something like this: update myTable set Col1=@col1, Col2=@col2 where ID=@ID if @@rowcount = 0 insert into myTable (Col1, Col2) values (@col1, @col2) My logic behind writing it in this way is that the update will perform an implicit select using the where clause and if that returns 0 then the insert will take place. The alternative to doing it this way would be to do a select and then based on the number of rows returned either do an update or insert. This I considered inefficient

Generate DEFAULT values in a CTE UPSERT using PostgreSQL 9.3

时光怂恿深爱的人放手 提交于 2019-11-26 18:28:31
问题 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

Using a if condition in an insert SQL Server

只谈情不闲聊 提交于 2019-11-26 15:33:21
I have the following statement in my code INSERT INTO #TProductSales (ProductID, StockQTY, ETA1) VALUES (@ProductID, @StockQTY, @ETA1) I want to do something like: IF @ProductID exists THEN UPDATE #TProductSales ELSE INSERT INTO #TProductSales Is there a way I can do this? The pattern is (without error handling): SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN TRANSACTION; UPDATE #TProductSales SET StockQty = @StockQty, ETA1 = @ETA1 WHERE ProductID = @ProductID; IF @@ROWCOUNT = 0 BEGIN INSERT #TProductSales(ProductID, StockQTY, ETA1) VALUES(@ProductID, @StockQTY, @ETA1); END COMMIT

Atomic UPSERT in SQL Server 2005

我怕爱的太早我们不能终老 提交于 2019-11-26 14:25:33
What is the correct pattern for doing an atomic "UPSERT" (UPDATE where exists, INSERT otherwise) in SQL Server 2005? I see a lot of code on SO (e.g. see Check if a row exists, otherwise insert ) with the following two-part pattern: UPDATE ... FROM ... WHERE <condition> -- race condition risk here IF @@ROWCOUNT = 0 INSERT ... or IF (SELECT COUNT(*) FROM ... WHERE <condition>) = 0 -- race condition risk here INSERT ... ELSE UPDATE ... where < condition > will be an evaluation of natural keys. None of the above approaches seem to deal well with concurrency. If I cannot have two rows with the same

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

旧时模样 提交于 2019-11-26 14:08:11
问题 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

Fast or Bulk Upsert in pymongo

て烟熏妆下的殇ゞ 提交于 2019-11-26 12:58:33
问题 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

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

安稳与你 提交于 2019-11-26 12:54:56
问题 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? 回答1: 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. 回答2: I'm currently working on such a statement

How to use RETURNING with ON CONFLICT in PostgreSQL?

给你一囗甜甜゛ 提交于 2019-11-26 11:40:52
I have the following UPSERT in PostgreSQL 9.5: INSERT INTO chats ("user", "contact", "name") VALUES ($1, $2, $3), ($2, $1, NULL) ON CONFLICT("user", "contact") DO NOTHING RETURNING id; If there are no conflicts it returns something like this: ---------- | id | ---------- 1 | 50 | ---------- 2 | 51 | ---------- But if there are conflicts it doesn't return any rows: ---------- | id | ---------- I want to return the new id columns if there are no conflicts or return the existing id columns of the conflicting columns. Can this be done? If so, how? I had exactly the same problem, and I solved it

How to resolve SQL0418N Error

醉酒当歌 提交于 2019-11-26 11:38:54
问题 I\'m using the statement below to update/insert some data to a table and, if I run it without parameters, it\'s fine. However, as soon as I try to execute it using parameters it throws: SQL0418N - A statement contains a use of an untyped parameter marker, the DEFAULT keyword, or a null value that is not valid . I\'ve read the error information here, but I\'m still struggling with why my statement won\'t execute. --This statement works MERGE Into AB.Testing_Table A USING (VALUES(\'TEST\', \'P\