Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

18
  • 11
    Yeah, I believe SQL Server's equivalent is called MERGE. In general, the concept is often referred to as "UPSERT".
    – chaos
    Commented Aug 2, 2009 at 13:40
  • 3
    @blub: If you create a unique key on geb and topic it will work (ALTER TABLE table ADD UNIQUE geb_by_topic (geb, topic)).
    – chaos
    Commented Aug 2, 2009 at 13:43
  • 1
    @Brian: Oracle's equivalent is also called MERGE but I'm not sure if its syntax is identical to SQL Server's.
    – Ken Keenan
    Commented Aug 2, 2009 at 14:20
  • 1
    @Brooks: If you pass it a 0, it will actually use 0 as the value. So don't do that. Don't pass it anything, or pass it a NULL, to allow the auto_increment behavior to work (which otherwise, yes, works as you presume; see dev.mysql.com/doc/refman/5.5/en/example-auto-increment.html).
    – chaos
    Commented May 17, 2012 at 16:56
  • 3
    Thank you, and you can use VALUES(col) to get the value from the insert argument in case of duplicate. Eg: ON DUPLICATE UPDATE b = VALUES(b), c = VALUES(c)
    – gerrytan
    Commented May 20, 2014 at 1:51