{"id":1870,"date":"2013-06-04T03:33:19","date_gmt":"2013-06-04T03:33:19","guid":{"rendered":"http:\/\/www.withdata.com\/ad\/?p=1870"},"modified":"2024-08-17T05:35:21","modified_gmt":"2024-08-17T05:35:21","slug":"replace-update-or-insert-a-row-into-sql-server-table","status":"publish","type":"post","link":"https:\/\/www.withdata.com\/blog\/sql-server\/replace-update-or-insert-a-row-into-sql-server-table.html","title":{"rendered":"Replace (Update\/Insert) a row into SQL Server table"},"content":{"rendered":"<p>In MySQL, \u00a0we use &#8220;ON DUPLICATE KEY UPDATE&#8221;\u00a0to either updates or inserts a row in a table.<\/p>\n<p>How to do it in SQL Server?<\/p>\n<p>Just like this:<\/p>\n<pre>if not exists (select 1 from employees where employee_id = 1)\r\n      insert into employees (employee_id,last_name,first_name) values ( 1,'smith', 'bob' )\r\nelse\r\n      update employees set last_name='smith' , first_name='bob' where employee_id = 1<\/pre>\n<p>See also:<\/p>\n<p><a href=\"http:\/\/www.withdata.com\/blog\/txttosql\/upsert-data-from-csv-file-to-sql-server-without-programming.html\"> Upsert data from CSV to SQL Server<\/a><br \/>\n<a href=\"http:\/\/www.withdata.com\/blog\/txttosql\/upsert-data-from-tsv-file-to-sql-server-without-programming.html\"> Upsert data from TSV to SQL Server<\/a><br \/>\n<a href=\"http:\/\/www.withdata.com\/blog\/txttosql\/upsert-data-from-txt-file-to-sql-server-without-programming.html\"> Upsert data from TXT to SQL Server<\/a><br \/>\n<a href=\"http:\/\/www.withdata.com\/blog\/xlstosql\/upsert-data-from-excel-file-to-sql-server-without-programming.html\"> Upsert data from Excel to SQL Server<\/a><br \/>\n<a href=\"http:\/\/www.withdata.com\/blog\/xmltomssql\/upsert-data-from-xml-file-to-sql-server-without-programming.html\"> Upsert data from XML to SQL Server<\/a><br \/>\n<a href=\"http:\/\/www.withdata.com\/blog\/jsontomssql\/upsert-data-from-json-file-to-sql-server-without-programming.html\"> Upsert data from JSON to SQL Server<\/a><br \/>\n<a href=\"http:\/\/www.withdata.com\/blog\/sqltomssql\/upsert-data-from-sql-file-to-sql-server-without-programming.html\"> Upsert data from SQL file to SQL Server<\/a><\/p>\n<p>Replace (Update\/Insert) a row into other DB:<\/p>\n<p>In Oracle,\u00a0<a href=\"https:\/\/www.withdata.com\/blog\/oracle\/replace-update-or-insert-a-row-into-oracle-table-merge-into.html\">https:\/\/www.withdata.com\/blog\/oracle\/replace-update-or-insert-a-row-into-oracle-table-merge-into.html<\/a><\/p>\n<p>In DB2,\u00a0<a href=\"https:\/\/www.withdata.com\/blog\/db2\/replace-update-or-insert-a-row-into-db2-table-merge-into.html\">https:\/\/www.withdata.com\/blog\/db2\/replace-update-or-insert-a-row-into-db2-table-merge-into.html<\/a><\/p>\n<p>In PostgreSQL,\u00a0<a href=\"https:\/\/www.withdata.com\/blog\/postgresql\/replace-update-or-insert-a-row-into-postgresql-table.html\">https:\/\/www.withdata.com\/blog\/postgresql\/replace-update-or-insert-a-row-into-postgresql-table.html<\/a><\/p>\n<p>In Sqlite,\u00a0<a href=\"https:\/\/www.withdata.com\/blog\/sqlite\/replace-update-or-insert-a-row-into-sqlite-table.html\">https:\/\/www.withdata.com\/blog\/sqlite\/replace-update-or-insert-a-row-into-sqlite-table.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MySQL, \u00a0we use &#8220;ON DUPLICATE KEY UPDATE&#8221;\u00a0to either updates or inserts a row in a table. How to do it in SQL Server? Just like this: if not exists (select 1 from employees where employee_id = 1) insert into employees (employee_id,last_name,first_name) values ( 1,&#8217;smith&#8217;, &#8216;bob&#8217; ) else update employees set last_name=&#8217;smith&#8217; , first_name=&#8217;bob&#8217; where [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[238,239,1321,428],"class_list":["post-1870","post","type-post","status-publish","format-standard","hentry","category-sql-server","tag-merge-into","tag-replace-into","tag-sql-server","tag-upsert"],"_links":{"self":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/1870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/comments?post=1870"}],"version-history":[{"count":13,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/1870\/revisions"}],"predecessor-version":[{"id":21731,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/1870\/revisions\/21731"}],"wp:attachment":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/media?parent=1870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/categories?post=1870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/tags?post=1870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}