Import Multiple Files to SQL Server Using DTSMarch 24, 2004 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. SimulationLet'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. FilesC:\MyImport\a.csv
1, MAK, A9411792711, 3400.25 C:\MyImport\b.csv
11, Rubon, 9671792711, 400.14 C:\MyImport\c.csv
69, Lucy, 8411992710, 305.11 TableCreate 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 PackageStep1: 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 (?) |