Home > Ask the SQL Server Experts > Roman Rehak - SQL Server Development and Reporting Services Questions & Answers > Stored procedure for a BULK INSERT to SQL Server 2005 text file
Ask The SQL Server Expert: Questions & Answers
EMAIL THIS

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

Roman Rehak EXPERT RESPONSE FROM: Roman Rehak

Pose a Question
Other SQL Server Categories
Meet all SQL Server Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 06 February 2008
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?

>
EXPERT RESPONSE
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.


Sound Off! -   Be the first to post a message to Sound Off!


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL Server stored procedures
Check SQL Server database and log file size with this stored procedure
SQL Server source code analysis and management adds database security
SQL and SQL Server Tutorial and Reference Guide
Configure SQL Server Service Broker for sending stored procedure data
Find size of SQL Server tables and other objects with stored procedure
Track changes to SQL Server 2000 and 2005 with one simple utility
Troubleshoot SQL Server 2005 temporary table performance problems
Use SQL Profiler to find long running stored procedures and commands
Stored procedure to monitor long-running jobs in SQL Server 2000
Using BULK INSERT to insert rows from SQL Server dataset to table

SQL Server 2005 (Yukon)
How to use rank function in SQL Server 2005
Manage traces in SQL Server 2005 Analysis Services with XMLA commands
Tutorial: SQL Server 2005 Analysis Services
Create a computed column in SQL Server using XML data
Open SSIS packages without validation using these SQL properties
Using the OUTPUT clause for practical SQL Server applications
Create DDL table in SQL Server 2005 to audit DDL trigger activity
How to process SQL Server 2005 Analysis Services for data availability
Configure SQL Server Service Broker for sending stored procedure data
SQL Server 2005 log shipping setup using the wizard
SQL Server 2005 (Yukon) Research

Roman Rehak - SQL Server Development and Reporting Services
Using BULK INSERT to insert rows from SQL Server dataset to table
SQL Server query to import database names
Troubleshoot SQL Server 2005 SP2 installation error
Workaround to Reporting Services error in SQL Server Express
How can I export stored procedures in SQL Server 2005?
Code to restore SQL Server databases in VB.NET
BULK INSERT to import data from file to SQL Server database
INSTEAD OF trigger to update a SQL Server table
Retrieve images from SQL Server and store in VB.Net
Can you import Excel data to SQL Server Reporting Services?

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
library  (SearchSQLServer.com)
trigger  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2005 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts