Skip to content

The Dummy Programmer

Stories of daily programming

  • Home
  • My other dummy projects
  • Games
    • Space Y: An Asteroids clone
  • Services
    • What’s my user agent
    • What’s my IP address
  • About
  • Toggle search form

Load a huge amount of data in a Sql Server table

Posted on March 20, 2019August 1, 2019 By

Last week I had to load a huge amount of data, about 500,000 rows, in a Sql Server table to make some data processing.

Using the “Sql Server Import and Export Data” tool this was an easy job.

After processing data in my own Sql Server, I had to reload the data to another Sql Server and execute the job again, but in this case I couldn’t use the “Sql Server Import and Export Data” tool.

How would you do that in a way that is as fast as using “Sql Server Import and Export Data” ?

For this purpose, you can use a command line utility called “bcp”, which stands for “bulk copy program”. This utility is usually installed along with Sql Server, but if it is not installed you can download here:

https://docs.microsoft.com/it-it/sql/tools/bcp-utility?view=sql-server-2017

 

So let’s start by creating some test data to make the job…

Create a test database in your Sql Server instance and execute the following script:

CREATE TABLE BCP_EXAMPLE
(
   ID INT PRIMARY KEY CLUSTERED IDENTITY(1,1),
   LAST_NAME VARCHAR(50),
   FIRST_NAME VARCHAR(50),
   PHONE_NUMBER VARCHAR(50),
   WEBSITE VARCHAR(250)
)
GO


DECLARE @I INT = 0

WHILE @I <= 500000
BEGIN
  INSERT INTO BCP_EXAMPLE(LAST_NAME, FIRST_NAME, PHONE_NUMBER, WEBSITE)
  VALUES(CONVERT(VARCHAR(50), @I), CONVERT(VARCHAR(50), @I), '+00' + CONVERT(VARCHAR(50), @I), 'http://www.' + CONVERT(VARCHAR(50), @I) + '.com')

  SET @I = @I + 1
END
GO

You will have noticed that making 500.000 inserts like in the example above takes a lot of time: on my PC takes about 2 minutes.

Now we will use bcp to export the content of the table to a file.

To do this open the Windows command prompt. The folder containing bcp is usually configured in the PATH environment variable, so you should be able to run it from wherever folder you are in, anyway for a default Sql Server 2017 installation you should find it at this location:

C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn

First of all, we have to create a format file. A format file is simply a file that contains the structure of the table you are copying.

To create a format file execute the following in the command prompt:

bcp BCP_EXAMPLE format nul -f"C:\Users\dummy\OneDrive\Desktop\test_bcp\BCP_EXAMPLE.fmt" -S (local) -d TestDB -U sa -P dummypwd

Some explanations:

  • BCP_EXAMPLE is the name of the table you want to create the format file
  • format nul indicates to bcp that you want to create a format file
  • -fC:\Users\…\test_bcp\BCP_EXAMPLE.fmt is path and file name of the format file
  • as you may guess, the other parameters are needed to connect to Sql Server

Note the double quotes around the path: you have to use them if the path contains spaces, otherwise you will get an error.

When you will execute the command above, you will be asked some “questions” about the columns of your table. You can usually leave the default value (which is the one you see in the square brackets) by pressing ENTER.

If you don’t want to confirm each field, you can use the “-n” option that makes bcp generate the format file without user intervention.

Now you can execute bcp to export the data in a file with the following command:

bcp BCP_EXAMPLE out "C:\Users\dummy\OneDrive\Desktop\test_bcp\BCP_EXAMPLE.dat" -f "C:\Users\dummy\OneDrive\Desktop\test_bcp\BCP_EXAMPLE.fmt" -S (local) -d TestDB -U sa -P dummypwd

Here the difference with the previous command:

  • out C:\Users\…test_bcp\BCP_EXAMPLE.dat is path and file name of the file that will contains the data of the table
  • -f C:\Users\…\test_bcp\BCP_EXAMPLE.fmt is the format file you just created above

You should see something similar:

bcp file export

 

To import the data to another server, you will need the following:

  • tha table must exist on the destination server
  • the format file and the data file you just created

So to complete the example, we will empty the table BCP_EXAMPLE and reload the data from the file generated with bcp.

Empty the table with this command:

TRUNCATE TABLE BCP_EXAMPLE

then launch the bcp command to import the data:

bcp BCP_EXAMPLE in "C:\Users\dummy\OneDrive\Desktop\test_bcp\BCP_EXAMPLE.dat" -f "C:\Users\dummy\OneDrive\Desktop\test_bcp\BCP_EXAMPLE.fmt" -S (local) -d TestDB -U sa -P dummypwd

And this is the result:

bcp file import

 

Both export and import took less then 5 seconds on my PC. A great result that becomes more evident when working with a greater number of lines.

Two suggestions before closing:

  • check Microsoft documentation to learn more about bcp
  • if you use bcp to make a backup of a table, keep with care also the format file: if you loose it you may not be able to import your data!

Have a nice day!

BCP, Sql Server

Post navigation

Previous Post: The ‘Microsoft.ACE.OLEDB.12.0’ provider is not registered on the local machine
Next Post: Dump the content of an array / hash in Perl

Related Posts

SQL Server LEN function return 0 passing a string with one space Sql Server
How to know who is blocking a query in Sql Server Sql Server
Improving SQL Server cursor performance Sql Server
Sql Server: apply an aggregate function to fields values in a row Sql Server
Merge two or more historical tables with Sql Server T-SQL – Part 1 Sql Server
SQL Server and SARGables predicates Sql Server

Search

Related posts

  • Sql Server error: Cannot construct data type date,…
  • Get all referencing tables of a table in Sql Server
  • Could not load file or assembly…
  • Reading data from a NetworkStream object in C#

Categories

  • .NET 6 (1)
  • .NET 7 (1)
  • AJAX (1)
  • Android (2)
  • Apache (4)
  • ASP.NET (9)
  • ASP.NET MVC (3)
  • Avalonia UI (1)
  • BCP (1)
  • Bitlocker (2)
  • C# (14)
  • CentOS (4)
  • ClosedXML (1)
  • CLR (1)
  • DNS (1)
  • Encryption (3)
  • Excel (2)
  • FuelPHP (3)
  • Games (2)
  • Google Chrome (1)
  • GSuite (1)
  • HTML (1)
  • Imagick (2)
  • Javascript (1)
  • Kindle (1)
  • LAMP (3)
  • Linux (7)
  • MariaDB (2)
  • Mathematics (2)
  • MySql (4)
  • NPOI (1)
  • Office 365 (1)
  • Perl (1)
  • PHP (6)
  • Programming (1)
  • Remote desktop (1)
  • SFTP (2)
  • Sockets (3)
  • Sql Server (20)
  • Sql Server 2016 (14)
  • Sql Server 2019 (1)
  • SSL (4)
  • Task scheduler (1)
  • Telerik ASP.NET AJAX (2)
  • The Dummy Programmer Chat (2)
  • Threading (5)
  • Tools (1)
  • TPL (3)
  • TypeScript (3)
  • Ubuntu (4)
  • Virtualization software (3)
  • Visual Studio (1)
  • Visual Studio Code (2)
  • VueJS (1)
  • Web fonts (1)
  • Web programming (6)
  • Windows (12)
  • Windows 10 (15)
  • Windows Forms (1)
  • Windows Server (6)

Copyright © 2024 The Dummy Programmer | Privacy Policy | Terms of use |

Powered by PressBook Masonry Dark

Manage Cookie Consent
This site doesn’t collect user personal data and doesn’t install profiling or analytical cookies, either its own or from third parties. Read our privacy policy for more info.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}