4 ms·
I am not the most advanced SQL developer by any means. Why is UPSERT needed/advantageous when you can do this? update [table] set ...; if @@ROWCOUNT = 0 ins
by pzxc 11y ago
I am not the most advanced SQL developer by any means. Why is UPSERT needed/advantageous when you can do this?
update [table] set ...; if @@ROWCOUNT = 0 insert into [table] ...
- Volundr 11y agoBecause I may want to insert a large number of rows. The above requires me to do so one by one, which is pretty inefficient. There are other ways to emulate upserts, but they tend to have performance and/or concurrency considerations.
- petergeoghegan 11y agoIt has race conditions. The only work-around that works (unless perhaps you have a 2-phase locking system, and are using serializable isolation level) involves subtransactions. For example: http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html#PLPGSQL-UPSERT-EXAMPLE http://www.postgresql.org/docs/current/static/plpgsql-contro... This is ugly an inefficient.
- deleted 11y ago[deleted]