{"id":413,"date":"2010-11-05T08:42:20","date_gmt":"2010-11-05T12:42:20","guid":{"rendered":"http:\/\/erics-test-lab.com\/?p=413"},"modified":"2010-11-05T08:42:20","modified_gmt":"2010-11-05T12:42:20","slug":"sql-synchronizing-identity-field-between-two-tables","status":"publish","type":"post","link":"https:\/\/www.esearing.com\/v2010\/2010\/11\/sql-synchronizing-identity-field-between-two-tables\/","title":{"rendered":"SQL &#8211; Synchronizing identity field between two tables"},"content":{"rendered":"<p>One thing that DBAs often have difficulty with is moving data from one database to another database. This may be to synchronize a  development environment with databases or tables on other servers with the same data values. Fortunately MSSQL provides a simple SQL solution.SET IDENTITY_INSERT that allows you to turn on and off the auto generation of the identity value .  The command gets turned on (turns off normal autonumber) prior to the inserting of data and gets turned off after the data has been inserted.<\/p>\n<p><code>\/\/ First we create a new record in our table<br \/>\nINSERT INTO dbo.tablename (FieldName) VALUES ('fieldvalue1')<br \/>\n\/\/ which creates record with ID 405 in our table.<br \/>\n&nbsp;<br \/>\n\/\/ Then we copy that data to our duplicate database using the<br \/>\n\/\/IDENTITY_INSERT attribute overriding the ID fields natural counter.<br \/>\nSET IDENTITY_INSERT dbo.nexttablename ON<br \/>\n&nbsp;<br \/>\nINSERT INTO dbo.nexttablename (ID,FieldName) VALUES (405, 'fieldvalue1')<br \/>\n\/\/OR<br \/>\nINSERT INTO dbo.nexttablename (ID, fieldvalue1)<br \/>\nSELECT * FROM dbo.tablename<br \/>\n&nbsp;<br \/>\nSET IDENTITY_INSERT dbo.nexttablename OFF<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One thing that DBAs often have difficulty with is moving data from one database to another database. This may be to synchronize a development environment with databases or tables on other servers with the same data values. Fortunately MSSQL provides &hellip; <a href=\"https:\/\/www.esearing.com\/v2010\/2010\/11\/sql-synchronizing-identity-field-between-two-tables\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,24,14],"tags":[],"class_list":["post-413","post","type-post","status-publish","format-standard","hentry","category-code","category-snippets-quick-code-tips-for-single-elements-attributes-or-details","category-sql"],"_links":{"self":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/comments?post=413"}],"version-history":[{"count":0,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/posts\/413\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/media?parent=413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/categories?post=413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.esearing.com\/v2010\/wp-json\/wp\/v2\/tags?post=413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}