{"id":813,"date":"2010-06-04T10:11:00","date_gmt":"2010-06-04T10:11:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/cesardelatorre\/2010\/06\/04\/importingexporting-data-to-sql-azure-databases-using-bcp-and-sql-scripts\/"},"modified":"2010-06-04T10:11:00","modified_gmt":"2010-06-04T10:11:00","slug":"importingexporting-data-to-sql-azure-databases-using-bcp-and-sql-scripts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/importingexporting-data-to-sql-azure-databases-using-bcp-and-sql-scripts\/","title":{"rendered":"Importing\/Exporting data to SQL Azure databases using BCP and SQL Scripts"},"content":{"rendered":"<h4><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/7608.image_2.png\"><img decoding=\"async\" height=\"74\" width=\"244\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/2480.image_thumb.png\" alt=\"image\" border=\"0\" title=\"image\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" \/><\/a> <\/h4>\n<h4>The basic way: SQL Scripts + BCP<\/h4>\n<p>There are also other methods you can use to export\/import or even sync data like <em>SQL Azure Migration Wizard, SQL Azure Data Sync,<\/em> and even <em>SSIS (SQL Server Integration Services).<\/em><\/p>\n<p>Also, instead of creating database schema using SQL Scripts, another new option is using the new <em>DACPAC (Data Tier Packages), in SQL Server 2008 R2, SQL Azure and Visual Studio 2010. <\/em><\/p>\n<p>But, in this post I&rsquo;m going to show the most basic way, but also, a way that you can instrument from execution scripts , etc.<\/p>\n<p>First of all, we need a SQL Script to apply to our empty <strong><em>SQL Azure<\/em><\/strong> database. <em><strong>SQL Server 2008 R2 Management Studio<\/strong><\/em> has a new convenient feature: &ldquo;<i>Generate Scripts for SQL Azure<\/i>&rdquo;.<\/p>\n<p>So, right-click on your SQL Server database and select Tasks &ndash;&gt; Generate Scripts&hellip; option:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/7041.clip_image002_2.jpg\"><img decoding=\"async\" height=\"458\" width=\"458\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/0160.clip_image002_thumb.jpg\" alt=\"clip_image002\" border=\"0\" title=\"clip_image002\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a><\/p>\n<p>Then, you&rsquo;ll be able to set the &lsquo;Scripting Options&rsquo;:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/5707.clip_image004_2.jpg\"><img decoding=\"async\" height=\"429\" width=\"461\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/3527.clip_image004_thumb.jpg\" alt=\"clip_image004\" border=\"0\" title=\"clip_image004\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a><\/p>\n<p>But, especially, we want to specify that the SQL Script must be generated for SQL Azure compatibility, therefore, you have to enter into the <i>Advanced Settings<\/i>, pressing the &lsquo;Advanced&rsquo; button:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/6607.clip_image006_2.jpg\"><img decoding=\"async\" height=\"426\" width=\"460\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/8713.clip_image006_thumb.jpg\" alt=\"clip_image006\" border=\"0\" title=\"clip_image006\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a><\/p>\n<p>So now, we have the SQL Script file containing our database schema, but specifically made for SQL Azure. We&rsquo;ll have something like the following script: <\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"1\">\n<tbody>\n<tr>\n<td width=\"576\" valign=\"top\">\n<p>\/****** Object: Table [dbo].[Product] Script Date: 06\/01\/2010 15:16:21 ******\/<\/p>\n<p>SET ANSI_NULLS ON<\/p>\n<p>GO<\/p>\n<p>SET QUOTED_IDENTIFIER ON<\/p>\n<p>GO<\/p>\n<p>CREATE TABLE [dbo].[Product](<\/p>\n<p>[ProductId] [int] IDENTITY(1,1) NOT NULL,<\/p>\n<p>[ProductDescription] [nvarchar](100) NULL,<\/p>\n<p>[UnitPrice] [money] NULL,<\/p>\n<p>[UnitAmount] [nvarchar](50) NULL,<\/p>\n<p>[Publisher] [nvarchar](200) NULL,<\/p>\n<p>[AmountInStock] [smallint] NULL,<\/p>\n<p>CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED<\/p>\n<p>&hellip; <\/p>\n<p>&hellip; <\/p>\n<p>&hellip; <\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Now, we&rsquo;re able to create our Database in SQL Azure and apply this SQL Script to it, so we&rsquo;ll have our tables.<\/p>\n<p>Go to Windows Azure portal and then to your SQL Azure site and create an empty database. In my case, I created a new database called &ldquo;NLayerApp&rdquo;:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/2772.clip_image008_2.jpg\"><img decoding=\"async\" height=\"479\" width=\"550\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/0184.clip_image008_thumb.jpg\" alt=\"clip_image008\" border=\"0\" title=\"clip_image008\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a><\/p>\n<p>Now we can connect to SQL Azure from SQL Server 2008 R2 Management Studio, providing the DNS server name:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/2678.clip_image009_2.png\"><img decoding=\"async\" height=\"279\" width=\"371\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/1050.clip_image009_thumb.png\" alt=\"clip_image009\" border=\"0\" title=\"clip_image009\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a><\/p>\n<p>And therefore, we can see our SQL Azure database already created:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/5633.clip_image011_2.jpg\"><img decoding=\"async\" height=\"328\" width=\"513\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/3187.clip_image011_thumb.jpg\" alt=\"clip_image011\" border=\"0\" title=\"clip_image011\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a><\/p>\n<p>Next step would be to execute our SQL Script, in order to create our tables and database objects. We create a new query and paste our file content within it:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/4263.clip_image012_2.png\"><img decoding=\"async\" height=\"352\" width=\"517\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/7367.clip_image012_thumb.png\" alt=\"clip_image012\" border=\"0\" title=\"clip_image012\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a><\/p>\n<p>We paste the script and then we execute it:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/4087.clip_image014_2.jpg\"><img decoding=\"async\" height=\"219\" width=\"520\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/1781.clip_image014_thumb.jpg\" alt=\"clip_image014\" border=\"0\" title=\"clip_image014\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a><\/p>\n<p>So, at the end, we can see our tables within the SQL Azure database:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/6406.clip_image015_2.png\"><img decoding=\"async\" height=\"448\" width=\"519\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/5305.clip_image015_thumb.png\" alt=\"clip_image015\" border=\"0\" title=\"clip_image015\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a><\/p>\n<p><b><\/b><\/p>\n<p><b>Using BCP.EXE to EXPORT\/IMPORT data<\/b><\/p>\n<p>The <strong><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms162802(SQL.105).aspx\">bcp utility<\/a><\/strong> is a command line utility that ships with Microsoft SQL Server. It bulk copies data between SQL Azure (or SQL Server) and a data file in a user-specified format. The bcp utility that ships with SQL Server 2008 R2 is fully supported by SQL Azure. <\/p>\n<p><b>You can use BCP to backup and restore your data on SQL Azure database.<\/b><\/p>\n<p>You can import large numbers of new rows into SQL Azure tables or export data out of tables into data files by using bcp. <\/p>\n<p>The bcp utility is not a migration tool. It does not extract or create any schema or format information from\/in a data file or your table. This means, if you use <strong>bcp<\/strong> to back up your data, make sure to create a schema or format file somewhere else to record the schema of the table you are backing up. <strong>bcp<\/strong> data files do not include any schema or format information, so if a table or view is dropped and you do not have a format file, you may be unable to import the data. The bcp utility has several command line arguments. For more information on the arguments, see <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms162802(SQL.105).aspx\">SQL Server Books Online<\/a> documentation. For more information on how to use bcp with views, see <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms187086(SQL.105).aspx\">Bulk Exporting Data from or Bulk Importing Data to a View<\/a>. <\/p>\n<p>Now, we can <strong>export data from our SQL Server database<\/strong>, using BCP.EXE, in this case using integrated security: <\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"1\">\n<tbody>\n<tr>\n<td width=\"576\" valign=\"top\">\n<p>bcp NLayerApp.dbo.Customer out &#8220;C:\\MyFolderPath\\Customer.txt&#8221; -T -c -S WIN7VS2010RC1\\SQLEXPRESS<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/5736.clip_image017_2.jpg\"><img decoding=\"async\" height=\"220\" width=\"587\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/6355.clip_image017_thumb.jpg\" alt=\"clip_image017\" border=\"0\" title=\"clip_image017\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a><\/p>\n<p>Then, we&rsquo;ll have a generated file with all the table&rsquo;s data.<\/p>\n<p>Now, we <strong>import that data into SQL-Azure database<\/strong>: <\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"1\">\n<tbody>\n<tr>\n<td width=\"576\" valign=\"top\">\n<p>bcp TestDB.dbo.Customer in &#8220;C:\\Users\\cesardl\\Desktop\\Arquitectura Marco .NET DPE Spain\\_Windows Azure V\\BCP\\Customer.txt&#8221; -c -U mysqlazureuser@mysqlazureservername -S tcp:mysqlazureservername.database.windows.net -P mypassword<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/0116.clip_image019_2.jpg\"><img decoding=\"async\" height=\"222\" width=\"593\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/6232.clip_image019_thumb.jpg\" alt=\"clip_image019\" border=\"0\" title=\"clip_image019\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a><\/p>\n<p>Now, we can check that data is already within the SQL Azure database:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/6710.clip_image021_2.jpg\"><img decoding=\"async\" height=\"291\" width=\"598\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/7658.clip_image021_thumb.jpg\" alt=\"clip_image021\" border=\"0\" title=\"clip_image021\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a><\/p>\n<p>Easy! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The basic way: SQL Scripts + BCP There are also other methods you can use to export\/import or even sync data like SQL Azure Migration Wizard, SQL Azure Data Sync, and even SSIS (SQL Server Integration Services). Also, instead of creating database schema using SQL Scripts, another new option is using the new DACPAC (Data [&hellip;]<\/p>\n","protected":false},"author":362,"featured_media":12806,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[90,91,114],"class_list":["post-813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cesardelatorre","tag-sql-azure","tag-sql-server-2008","tag-window-azure"],"acf":[],"blog_post_summary":"<p>The basic way: SQL Scripts + BCP There are also other methods you can use to export\/import or even sync data like SQL Azure Migration Wizard, SQL Azure Data Sync, and even SSIS (SQL Server Integration Services). Also, instead of creating database schema using SQL Scripts, another new option is using the new DACPAC (Data [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts\/813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/comments?post=813"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts\/813\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/media\/12806"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/media?parent=813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/categories?post=813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/tags?post=813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}