Ask the Expert

Stored procedure for a BULK INSERT to SQL Server 2005 text file

Can you please help me write a stored procedure for a BULK INSERT to a SQL Server 2005 text file? How can I import to multiple tables with only one insert, in order to maintain referential integrity?

    Requires Free Membership to View

In this case I would recommend creating a temporary table and using the BULK INSERT command to populate it with the data from the text file. BULK INSERT is very fast and if the destination table is empty and has no indexes, you can load millions of rows within seconds. For more details about BULK INSERT, see "BULK insert to import data from file to SQL Server database."

Once you have data in the temporary table, you can use series of INSERT, UPDATE and DELETE statements to populate other tables in the database. In some scenarios I often add an ID column to the temporary table, and then use the UPDATE…FROM statement to join permanent tables on one or more columns in the temporary table and populate the ID column. After that, I know that any rows in the temporary table where the ID equals NULL are new rows and need to be inserted. The rows where the ID column has a value are considered a match. For those rows I use another UPDATE...FROM statement where I join on the ID column and copy the values from the temporary table to the existing rows. This technique is very useful for data imports using set-based operations rather than row-by-row processing.

This was first published in February 2008

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: