Ask the Expert

BULK INSERT to import data from file to SQL Server database

I would like a stored procedure to import data from a .txt or .xls file to a database using SQL Server. I will use temporary tables to copy from the file and then I'll transfer from the temporary tables to the database. Could you please give me an example of such a procedure?

    Requires Free Membership to View

There are many ways to import data into SQL Server and what you choose depends greatly on your specific scenario. If you want to simply load contents of a text file into a temporary table, the BULK INSERT command should do the trick. If the number and the order of the columns in the file is the same as in the destination table, your statement could be as simple as this:

BULK INSERT MyTable
FROM 'c:datamydata.txt'
WITH
(
FIELDTERMINATOR =' |',
ROWTERMINATOR =' |\n'
)

If you want to exclude some columns or modify the order of columns, create a format file and specify it as a parameter to BULK INSERT. You can refer to Books Online to learn more about how to use BULK INSERT and how to create a format file.

This was first published in December 2007

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: