{"id":55,"date":"2006-07-11T12:37:44","date_gmt":"2006-07-11T17:37:44","guid":{"rendered":"http:\/\/stein.everybody.org\/brain\/sql-idempotent-insert\/"},"modified":"2011-03-10T10:57:33","modified_gmt":"2011-03-10T15:57:33","slug":"sql-idempotent-insert","status":"publish","type":"post","link":"https:\/\/jeremystein.com\/brain\/sql-idempotent-insert\/","title":{"rendered":"SQL Idempotent Insert"},"content":{"rendered":"<p>Sometimes you want to perform an idempotent insert in SQL.  Here are some of the options (using SQL Server&#8217;s TSQL syntax):<\/p>\n<ul>\n<li>Check for existence first:<br \/>\n<code>if not exists (select 'x' from tbl where key=val) insert into tbl values (val, ...)<\/code><\/li>\n<li>Delete and then insert (but this will fail if there are foreign keys depending on it<\/li>\n<li>Wrap the insert statement in something that can catch errors (like in a stored procedure).  Ignore errors caused by the row already existing.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you want to perform an idempotent insert in SQL. Here are some of the options (using SQL Server&#8217;s TSQL syntax): Check for existence first: if not exists (select &#8216;x&#8217; from tbl where key=val) insert into tbl values (val, &#8230;) Delete and then insert (but this will fail if there are foreign keys depending on [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"_links":{"self":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":0,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"wp:attachment":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}