July 19, 2014 at 7:22 pm
I've got a foreach loop container looping through a folder that has zipped files with both a 2014-06-20 and 2014-06-21 date as part of their filenames. I want to unzip the Jun 20 ones first, the Jun 21 ones second. I've got it working to the point that first the filenames having 2014-06-20 are getting unzipped, but after that I don't know how to make it terminate the foreach loop so that I can update the Timetbl with to show IsProcessed = 1 for the 2014-06-20 date.
How do you break out of a foreach loop once every file meeting the date requirement has been processed once?
Here's what I have so far:
SourceFolder looks like this:
File1_2014-06-20.txt.zip
File1_2014-06-21.txt.zip
File2_2014-06-20.txt.zip
File2_2014-06-21.txt.zip
File3_2014-06-20.txt.zip
File3_2014-06-20.txt.zip
I created a TimeTbl to contain all dates, with column as to whether that date was processed or not. DDL:
CREATE TABLE [dbo].[Timetbl](
[TimeCol] [datetime] NULL,
[IsProcessed] [bit] NULL
) ON [PRIMARY]
GO
inserted into table, 4 rows
insert into Timetbl
values
('2014-06-18', 1),
('2014-06-19', 1),
('2014-06-20', 0),
('2014-06-21', 0);
PACKAGE
2 package level variables:
name:datePart\scope: Package1\datatype: string
name: zippedFile\scope: Package1\datatype: string
Control Flow:
Execute SQL Task: make a Datepart to use in Foreach Loop containers FileNameRetrieval expression
General
Result Set: single row
connectionType: oledb
sqlsourcetype: direct input
sqlstatement:
select top 1 CONVERT(varchar(10), TimeCol, 20) as TimeCol
from Timetbl
where IsProcessed = 0
order by timecol asc
Parameter Mapping
[nothing]
Result Set
Result Name: TimeCol
Variable Name: User::datePart
Expressions
[nothing]
Foreach Loop Container
Collection
Enumerator: Foreach File Enumerator
Expressions: Expressions: FileNameRetrieval\"*"+@[User::datePart]+".txt.zip"
Folder: C:\SourceFolder
Files: *.txt.zip
VariableMappings:
variable: User::zippedFile\index:0
Inside the Foreach Loop Container
Execute Process Task Editor
Process
Executable C:\Program Files (x86)\7-Zip\7z.exe
Arguments e -oC:\Users\me\Downloads\test2
Expressions
Arguments\"e " +@[User::zippedFile]+ " " +"-oC:\\Users\\me\\Downloads\\test2"
July 29, 2014 at 10:38 am
A foreach loop in SSIS is, by definition, an iteration of n items, in which n is defined at the beginning of the loop. So you can't really exit a foreach loop prematurely. However, you could configure your package to effectively do nothing in a loop.
I blogged about the scenario you describe here some time back: http://www.timmitchell.net/post/2009/09/16/skipping-items-in-a-foreach-loop/. Essentially, you can use an SSIS expression on your precedence constraint to check some condition before executing anything else in that iteration of the loop. You could interrogate your date table that you mentioned in each loop and see if the current date has already been loaded. You wouldn't be terminating the loop early - the loop would still execute once for each file in the target directory. However, binding an expression to that precedence constraint would allow you to conditionally skip files representing dates that have already been processed.
Hope this helps!
Tim Mitchell, Microsoft Data Platform MVP
Data Warehouse and ETL Consultant
TimMitchell.net | @Tim_Mitchell | Tyleris.com
ETL Best Practices
July 29, 2014 at 1:22 pm
Tim Mitchell (7/29/2014)
A foreach loop in SSIS is, by definition, an iteration of n items, in which n is defined at the beginning of the loop. So you can't really exit a foreach loop prematurely. However, you could configure your package to effectively do nothing in a loop.I blogged about the scenario you describe here some time back: http://www.timmitchell.net/post/2009/09/16/skipping-items-in-a-foreach-loop/. Essentially, you can use an SSIS expression on your precedence constraint to check some condition before executing anything else in that iteration of the loop. You could interrogate your date table that you mentioned in each loop and see if the current date has already been loaded. You wouldn't be terminating the loop early - the loop would still execute once for each file in the target directory. However, binding an expression to that precedence constraint would allow you to conditionally skip files representing dates that have already been processed.
Hope this helps!
Just making your link easier to use:
http://www.timmitchell.net/post/2009/09/16/skipping-items-in-a-foreach-loop/
July 29, 2014 at 1:26 pm
Thanks Lynn - forgot to add the hyperlink.
Tim Mitchell, Microsoft Data Platform MVP
Data Warehouse and ETL Consultant
TimMitchell.net | @Tim_Mitchell | Tyleris.com
ETL Best Practices
July 29, 2014 at 1:37 pm
Tim Mitchell (7/29/2014)
Thanks Lynn - forgot to add the hyperlink.
Not a problem. Glad to help out.
July 29, 2014 at 1:59 pm
Tim Mitchell (7/29/2014)
A foreach loop in SSIS is, by definition, an iteration of n items, in which n is defined at the beginning of the loop. So you can't really exit a foreach loop prematurely. However, you could configure your package to effectively do nothing in a loop.I blogged about the scenario you describe here some time back: http://www.timmitchell.net/post/2009/09/16/skipping-items-in-a-foreach-loop/. Essentially, you can use an SSIS expression on your precedence constraint to check some condition before executing anything else in that iteration of the loop. You could interrogate your date table that you mentioned in each loop and see if the current date has already been loaded. You wouldn't be terminating the loop early - the loop would still execute once for each file in the target directory. However, binding an expression to that precedence constraint would allow you to conditionally skip files representing dates that have already been processed.
Hope this helps!
That's basically what I had to do in a number of packages I'm working with now.
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
July 29, 2014 at 4:37 pm
It sounds a lot like you really need to have two nested loops,
Loop 1 ) foreach date
.. Loop 2) foreach file matching date
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply