Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how do you break out of a foreach loop Expand / Collapse
Author
Message
Posted Saturday, July 19, 2014 7:22 PM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Tuesday, October 21, 2014 9:00 PM
Points: 680, Visits: 1,598
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"
Post #1594354
Posted Tuesday, July 29, 2014 10:38 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, October 22, 2014 12:46 PM
Points: 1,046, Visits: 2,734
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, SQL Server MVP
Independent Business Intelligence Consultant
www.TimMitchell.net
@Tim_Mitchell

Post #1597400
Posted Tuesday, July 29, 2014 1:22 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 9:09 PM
Points: 20,739, Visits: 32,528
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/



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1597472
Posted Tuesday, July 29, 2014 1:26 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, October 22, 2014 12:46 PM
Points: 1,046, Visits: 2,734
Thanks Lynn - forgot to add the hyperlink.



Tim Mitchell, SQL Server MVP
Independent Business Intelligence Consultant
www.TimMitchell.net
@Tim_Mitchell

Post #1597475
Posted Tuesday, July 29, 2014 1:37 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 9:09 PM
Points: 20,739, Visits: 32,528
Tim Mitchell (7/29/2014)
Thanks Lynn - forgot to add the hyperlink.


Not a problem. Glad to help out.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1597480
Posted Tuesday, July 29, 2014 1:59 PM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: 2 days ago @ 3:44 PM
Points: 1,452, Visits: 8,255
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.




Alvin Ramard
Memphis PASS Chapter

All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.
Post #1597485
Posted Tuesday, July 29, 2014 4:37 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 5:32 PM
Points: 1,796, Visits: 5,801
It sounds a lot like you really need to have two nested loops,

Loop 1 ) foreach date
.. Loop 2) foreach file matching date



MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1597523
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse