August 31, 2011 at 11:44 am
This is my first attempt at SSIS, unfortunately its for work. I have a folder with csv's in it, i need a script to loop through this and import into the database once every month.
I have managed to script the bulk insert, but how do i now loop through the files and pass the name of each file into the SP that runs the bulk insert and run on a schedule.
Many thanks
Darryl Wilson
darrylw99@hotmail.com
August 31, 2011 at 1:25 pm
Use the for each loop container.
August 31, 2011 at 1:42 pm
I am totally new to SSIS, i have no idea even where to start.
I know i need to loop through list of files in a folder and i need to run a package every month, but after that i am lost.
DW
Darryl Wilson
darrylw99@hotmail.com
August 31, 2011 at 1:45 pm
August 31, 2011 at 3:26 pm
You can loop through the directory using SSIS, which isn't too crazy to do, but if you're still learning SSIS and just need a quick and dirty solution you can use xp_cmdshell if you have it enabled on the server.
Here's an example:
DECLARE @tmpDIR TABLE (DirOutput NVARCHAR(255))
INSERT INTO @tmpDIR
EXECUTE xp_cmdshell N'dir e:\test1'
SELECT RIGHT(DirOutput,LEN(DirOutput)-39) FROM @tmpDIR WHERE DirOutput LIKE '%.txt'
And your output will be all files in E:\Test1 with TXT extension. You may need to play with the spacing as on our server the DIR listing is fixed width with 39 characters to the left of the file name, but I think it may be alittle different depending on which version of windows you're using. But either way once you get the DIR listing into SQL you can parse it however you please.
For more info on xp_cmdshell see http://msdn.microsoft.com/en-us/library/ms175046.aspx since there can be some security boo-yeah's you might want to read-up on.
Sam
September 1, 2011 at 4:24 am
sam.alexander (8/31/2011)
You can loop through the directory using SSIS, which isn't too crazy to do, but if you're still learning SSIS and just need a quick and dirty solution you can use xp_cmdshell if you have it enabled on the server.Here's an example:
DECLARE @tmpDIR TABLE (DirOutput NVARCHAR(255))
INSERT INTO @tmpDIR
EXECUTE xp_cmdshell N'dir e:\test1'
SELECT RIGHT(DirOutput,LEN(DirOutput)-39) FROM @tmpDIR WHERE DirOutput LIKE '%.txt'
And your output will be all files in E:\Test1 with TXT extension. You may need to play with the spacing as on our server the DIR listing is fixed width with 39 characters to the left of the file name, but I think it may be alittle different depending on which version of windows you're using. But either way once you get the DIR listing into SQL you can parse it however you please.
For more info on xp_cmdshell see http://msdn.microsoft.com/en-us/library/ms175046.aspx since there can be some security boo-yeah's you might want to read-up on.
Sam
::--EXECUTE xp_cmdshell N'dir e:\test1'
---------------------
use it like this
EXECUTE xp_cmdshell N'dir e:\test1\*.csv /b'
and it will be better if you use BCP instaead of bulk nsert in this case..
Regards
Ashok
September 1, 2011 at 5:04 am
I have managed to create a package and loop through the files by dragging a For..loop container. then added a data flow task. but the next 3 things i need to do are baffling me:
i have created a variable filename (which hopefully) contains the name of the interated file:
1. How can i insert this into a field in my output database.
2. How can I insert this filename into another table (as a lookup).
Also I had to change the variable type in my database to varchar instead of date because my input data is in the form of yyyymmdd (20110101). Is it possbible to convert this before it's inserted.
With the filename that comes in, is it possible to say if file like '__BB__.csv' then do this data flow task or if file like '__ITEM__' then do this other data flow task.
Many thanks again.
Darryl Wilson
darrylw99@hotmail.com
September 1, 2011 at 8:24 am
Variable Information
I am not clear what you are asking on questions 1 and 2.
Changing the date can be done using the Data conversion flow task.
The for each loop conatiner has and enumeration configuration that will allow you to say only look for files like *BB*.csv. Using this method you would need two for each containers one for each file "type".
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply