Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

MS SQL

Posted Mar 24, 2004

Import Multiple Files to SQL Server Using DTS

By Muthusamy Anantha Kumar aka The MAK

In the typical IT environment, it is often necessary to import flat files to SQL Server tables. Sometimes it is necessary to import many files at the same time from same or different folders. In my previous article, I mentioned how to import such files using BULK Insert and BCP utility. In this article, I am going to discuss how to import all the files from a particular folder to SQL Server using a batch file and DTS package.

Simulation

Let's simulate the whole scenario for importing multiple files. First, let's create a folder C:\MyImport and create 3 files, a.csv, b.csv and c.csv with the below content. Also, create a table in SQL Server to hold the imported data.

Files

C:\MyImport\a.csv

1, MAK, A9411792711, 3400.25
2, Claire, A9411452711, 24000.33
3, Sam, A5611792711, 1200.34
4, Wright, A5611792711, 1200.34
5, Richard, G561d792755, 1223.34
6, Valarie, B5611792788, 1240.32

C:\MyImport\b.csv

11, Rubon, 9671792711, 400.14
22, Mike, 9418952711, 4000.56
39, Hsu, 75611792511, 1230.00

C:\MyImport\c.csv

69, Lucy, 8411992710, 305.11
45, Grace, 3413452713, 246.52
33, Saint, 5461795716, 1278.70

Table


Create Database Bank
Go
Use Bank
go
Create table Account([ID] int, Name Varchar(100), 
AccountNo varchar(100), Balance money)
Go
Create table logtable (id int identity(1,1), 
	Status varchar(500), 
	Importeddate datetime default getdate())
Go
use master
go
sp_addlogin 'importuser','import','Bank'
go
use Bank
go
sp_adduser 'importuser'
go
sp_addrolemember 'db_datareader','importuser'
go
sp_addrolemember 'db_datawriter','importuser'
go

Create DTS Package

Step1: Create Global variables FileName, ServerName and DatabaseName in the DTS package as shown below.

Step 2: Create connections and transformation.

Create the Text connection and SQL Server connection and add the transformation displayed below.

Step 3: Complete the mapping as shown below.

Step 4: Add dynamic tasks and link the "SQLServer" connection to the global variable "ServerName", link "inputfile" to the global variable "FileName" and link "initial catalog" in "SqlServer" connection to "DatabaseName".

Step 5: Add Success workflow between Dynamic Properties and InputFile Connection .

Step 6: Add a SQL Task to write to the logtable as shown below

SQL

Insert into LogTable (Status) values (?)


MS SQL Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM


















Thanks for your registration, follow us on our social networks to keep up-to-date