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.


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


RELATED CONTENT
SQL Server stored procedures
SQL Server trigger vs. stored procedure to receive data notification
How to use SQL Server 2008 hierarchyid data type
SQL and SQL Server Tutorial and Reference Guide
SQL Server stored procedures tutorial: Write, tune and get examples
Check SQL Server database and log file size with this stored procedure
SQL Server source code analysis and management adds database security
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

SQL Server 2005 (Yukon)
Tips for tuning SQL Server 2005 to improve reporting performance
Using DATEADD and DATEDIFF to calculate SQL Server datetime values
SQL Server consolidation: Why it's an optimization technique
Parent-child dimensions in SQL Server 2005 with Analysis Services MDX
Using SQL Server datetime functions GETDATE, DATENAME and DATEPART
SSIS error message due to installation problem on SQL Server 2005
SQL Server data conversions from date/time values to character types
Basics for working with DATETIME and SMALLDATETIME in SQL Server 2005
How to configure Database Mail in SQL Server 2005 to send mail
How to use rank function in SQL Server 2005
SQL Server 2005 (Yukon) Research

Roman Rehak - SQL Server Development and Reporting Services
Tips for tuning SQL Server 2005 to improve reporting performance
Manipulate column names in a SQL Server table
Licensing a standby server for SQL Server replication
SQL Server trigger vs. stored procedure to receive data notification
Storing and searching for image files in SQL Server
Can I encrypt and restore a database backup in SQL Server 2005?
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

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



SQL Solutions - SQL Database Design
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