I have a massive flat file having hundreds of millions rows. I only want to insert the top 1000 of them. How can I do this?
Sql-server – Bulk insert top 1000 rows from a big flat file
bcpbulk-insertsql server
Related Question
- Sql-server – SQL Server 2005 – Generate and Bulk Insert 3 million rows across 2 tables
- Sql-server – Insert into table select * from table vs bulk insert
- Mysql – for Insert multiple rows in one single insert MySQL
- Sql-server – A column is missing from the Select Top 1000 Rows result
- Sql-server – Bulk insert inserting 0 rows
- Sql-server – How to copy table content before deleting to flat file using BCP
- Sql-server – Bulk insert from a complex text file into a table
- Sql-server – BULK INSERT continue on PRIMARY KEY error
Best Answer
bcp Import
You can import data from your huge data file using Microsoft SQL Server's bcp utility from the command prompt.
bcp Description
Example Syntax
This translate to:
bcp
bulk copy program
YOUR_TABLE
The table you will be importing your data to
-d YOUR_DB
The name of the database you are connecting to
-i INPUT_FILE
The full pathname and filename to your huge file (e.g. C:\temp\bigfile.txt)
-S YOUR_SERVER
The name of the server you are connecting to
-F number
The first line to import
L number
The last line to import
-U YOUR_LOGIN
The SQL Server Login (alternatively you can use -T for a trusted connection)
-P YOUR_PASSWORD
The password for the SQL Server Login
-f FORMAT_FILE
Depending on your table definition you will probably require a format file which defines the structure of the table you are importing data to and the delimiters in the INPUT_FILE. Further information for the format file can be found here and here.
Your command should then look like this:
bcp format file
Depending on the complexity of your table and input file, you might have to create a separate bcp format file to accommodate missing columns in either the input file, target table or both. This can be achieved by exporting your table without a format file:
bcp will ask you some questions about your fields and terminators for the format file and then will ask you to provide a name:
You now have a format file for your target table which can be used in the bcp import.
The format file will look a bit like this one:
This is just a basic primer on how to use the bcp utility to import data from a file.
Answer
References