upsert

How to Perform an UPSERT so that I can use both new and old values in update part

拜拜、爱过 提交于 2019-11-26 10:29:38
Stupid but simple example: Assume I have a table 'Item' where I keeps totals of the items that receive. Item_Name Items_In_Stock Item name is primary key here. How to i achieve the following when ever I receive item A in quantity X. If the item does not exist, I insert a new recored for Item A and set the items in stock to X and if there exists a record where items in stock was Y then the new value in items in stock is (X + Y) INSERT INTO `item` (`item_name`, items_in_stock) VALUES( 'A', 27) ON DUPLICATE KEY UPDATE `new_items_count` = 27 + (SELECT items_in_stock where item_name = 'A' ) My

syntax for single row MERGE / upsert in SQL Server

雨燕双飞 提交于 2019-11-26 09:24:46
问题 I\'m trying to do a single row insert/update on a table but all the examples out there are for sets. Can anyone fix my syntax please: MERGE member_topic ON mt_member = 0 AND 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\') Resolution per marc_s is to convert the single row to a subquery - which makes me think the MERGE command is not really intended for single row upserts. MERGE member_topic

How do I UPDATE a row in a table or INSERT it if it doesn't exist?

老子叫甜甜 提交于 2019-11-26 09:07:02
问题 I have the following table of counters: CREATE TABLE cache ( key text PRIMARY KEY, generation int ); I would like to increment one of the counters, or set it to zero if the corresponding row doesn\'t exist yet. Is there a way to do this without concurrency issues in standard SQL? The operation is sometimes part of a transaction, sometimes separate. The SQL must run unmodified on SQLite, PostgreSQL and MySQL, if possible. A search yielded several ideas which either suffer from concurrency

SQLite UPSERT / UPDATE OR INSERT

爷,独闯天下 提交于 2019-11-26 08:44:19
问题 I need to perform UPSERT / INSERT OR UPDATE against a SQLite Database. There is the command INSERT OR REPLACE which in many cases can be useful. But if you want to keep your id\'s with autoincrement in place because of foreign keys, it does not work since it deletes the row, creates a new one and consequently this new row has a new ID. This would be the table: players - (primary key on id, user_name unique) | id | user_name | age | ------------------------------ | 1982 | johnny | 23 | | 1983

Does DB2 have an “insert or update” statement?

怎甘沉沦 提交于 2019-11-26 08:09:28
问题 From my code (Java) I want to ensure that a row exists in the database (DB2) after my code is executed. My code now does a select and if no result is returned it does an insert . I really don\'t like this code since it exposes me to concurrency issues when running in a multi-threaded environment. What I would like to do is to put this logic in DB2 instead of in my Java code. Does DB2 have an insert-or-update statement? Or anything like it that I can use? For example: insertupdate into mytable

Insert Update stored proc on SQL Server

帅比萌擦擦* 提交于 2019-11-26 07:20:41
问题 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

Using a if condition in an insert SQL Server

孤街醉人 提交于 2019-11-26 04:28:23
问题 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? 回答1: 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

Atomic UPSERT in SQL Server 2005

旧城冷巷雨未停 提交于 2019-11-26 03:54:34
问题 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.

Upserting in MS-access

情到浓时终转凉″ 提交于 2019-11-26 03:19:08
问题 I need to write an SQL query for MS-Access 2000 so that a row is updated if it exists, but inserted if it does not. (I believe this is called an \"upsert\") i.e. If row exists... UPDATE Table1 SET (...) WHERE Column1=\'SomeValue\' If it does not exist... INSERT INTO Table1 VALUES (...) Can this be done in one query? 回答1: I usually run the insert statement first and then I check to see if error 3022 occurred, which indicates the row already exists. So something like this: On Error Resume Next

Insert, on duplicate update in PostgreSQL?

廉价感情. 提交于 2019-11-26 03:12:54
问题 Several months ago I learned from an answer on Stack Overflow how to perform multiple updates at once in MySQL using the following syntax: INSERT INTO table (id, field, field2) VALUES (1, A, X), (2, B, Y), (3, C, Z) ON DUPLICATE KEY UPDATE field=VALUES(Col1), field2=VALUES(Col2); I\'ve now switched over to PostgreSQL and apparently this is not correct. It\'s referring to all the correct tables so I assume it\'s a matter of different keywords being used but I\'m not sure where in the