{"id":1245,"date":"2011-11-21T01:07:12","date_gmt":"2011-11-21T01:07:12","guid":{"rendered":"http:\/\/craftydba.com\/?p=1245"},"modified":"2011-11-24T21:17:40","modified_gmt":"2011-11-24T21:17:40","slug":"import-export-data-part-1","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=1245","title":{"rendered":"Import &#038; Export Data &#8211; Part 1"},"content":{"rendered":"<p>There are not many real life databases that do not import data from external sources or do not export data for delivery to external targets.  I am going to devote a series of talks on different ways to perform these functions.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms174335.aspx\">INSERT<\/a> statement is the easiest way to load small sets of static information into a database.  We will be working again with the Boy Scouts of America (BSA) hypothetical database.<\/p>\n<p>The first example loads the rank table with a data.  We can see the auto-increment surrogote key is incremented.<\/p>\n<pre><span style=\"color: #008000;\">-- Use new rowset notation\r\nINSERT INTO [RECENT].[TBL_RANK] ([RANK_DESC]) VALUES\r\n('Scout'), \r\n('Tenderfoot'),\r\n('Second Class'),\r\n('First Class'),\r\n('Star'),\r\n('Life'),\r\n('Eagle'),\r\n('Bronze Palms'),\r\n('Gold Palms'),\r\n('Silver Palms');\r\nGO\r\n\r\n-- Show the rows \r\nSELECT RANK_ID, RANK_DESC FROM [RECENT].[TBL_RANK];\r\nGO<\/span><\/pre>\n<\/p>\n<p>On interesting thing about an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms186775.aspx\">identity <\/a>column is that seed value is not reset with the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms189835.aspx\">DELETE<\/a> statement.  The example below shows the second set of inserts are given values of 11 to 20.<\/p>\n<pre><span style=\"color: #008000;\">-- Delete does not reset the identity column\r\nDELETE FROM [RECENT].[TBL_RANK];\r\n\r\n-- Use new rowset notation\r\nINSERT INTO [RECENT].[TBL_RANK] ([RANK_DESC]) VALUES\r\n('Scout'), \r\n('Tenderfoot'),\r\n('Second Class'),\r\n('First Class'),\r\n('Star'),\r\n('Life'),\r\n('Eagle'),\r\n('Bronze Palms'),\r\n('Gold Palms'),\r\n('Silver Palms');\r\nGO\r\n\r\n-- Show the rows \r\nSELECT RANK_ID, RANK_DESC FROM [RECENT].[TBL_RANK];\r\nGO<\/span><\/pre>\n<\/p>\n<p>On the other hand, the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms177570.aspx\">TRUNCATE TABLE<\/a> statement does reset the seed value of the table.  The example below shows the third set of inserts which are given values 1 to 10.<\/p>\n<pre><span style=\"color: #008000;\">-- Truncate resets the identity column\r\nTRUNCATE TABLE [RECENT].[TBL_RANK];\r\n\r\n-- Use new rowset notation\r\nINSERT INTO [RECENT].[TBL_RANK] ([RANK_DESC]) VALUES\r\n('Scout'), \r\n('Tenderfoot'),\r\n('Second Class'),\r\n('First Class'),\r\n('Star'),\r\n('Life'),\r\n('Eagle'),\r\n('Bronze Palms'),\r\n('Gold Palms'),\r\n('Silver Palms');\r\nGO\r\n\r\n-- Show the rows \r\nSELECT RANK_ID, RANK_DESC FROM [RECENT].[TBL_RANK];\r\nGO<\/span><\/pre>\n<\/p>\n<p>Importing data by using INSERT statements is a good technique for small sets of static data.  The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms162773.aspx\">sqlcmd utility<\/a> can be used to automate the execution of these statements from a batch program.  <\/p>\n<p>With the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms175987.aspx\">FULL recovery model<\/a>, every INSERT statement has an entry sent to the transaction log.  Because of this overhead, loading large amounts of data with INSERT statements is not recommended.<\/p>\n<p>Next time, I will be talking about the BULK INSERT statement that can be used to load large amounts of data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are not many real life databases that do not import data from external sources or do not export data for delivery to external targets. I am going to devote a series of talks on different ways to perform these functions. The INSERT statement is the easiest way to load small sets of static information into a database. We will be working again with the Boy Scouts of America (BSA) hypothetical database. The first example loads the rank table with a data. We can see the auto-increment surrogote key is&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[249,12,248,30,15,28,250,251,29],"class_list":["post-1245","post","type-post","status-publish","format-standard","hentry","category-db-dev","tag-export","tag-free-code","tag-import","tag-insert","tag-john-f-miner-iii","tag-sql-server","tag-sqlcmd-utility","tag-truncate-table","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/1245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1245"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/1245\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}