SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how do you break out of a foreach loop


how do you break out of a foreach loop

Author
Message
KoldCoffee
KoldCoffee
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1555 Visits: 1905
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"
Tim Mitchell
Tim Mitchell
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1837 Visits: 2988
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


Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40446 Visits: 38567
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/

Cool
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)
Tim Mitchell
Tim Mitchell
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1837 Visits: 2988
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


Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40446 Visits: 38567
Tim Mitchell (7/29/2014)
Thanks Lynn - forgot to add the hyperlink.


Not a problem. Glad to help out.

Cool
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)
Alvin Ramard
Alvin Ramard
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4245 Visits: 11639
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.

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
mister.magoo
mister.magoo
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4196 Visits: 7865
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);




  • 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

  • Go


    Permissions

    You can't post new topics.
    You can't post topic replies.
    You can't post new polls.
    You can't post replies to polls.
    You can't edit your own topics.
    You can't delete your own topics.
    You can't edit other topics.
    You can't delete other topics.
    You can't edit your own posts.
    You can't edit other posts.
    You can't delete your own posts.
    You can't delete other posts.
    You can't post events.
    You can't edit your own events.
    You can't edit other events.
    You can't delete your own events.
    You can't delete other events.
    You can't send private messages.
    You can't send emails.
    You can read topics.
    You can't vote in polls.
    You can't upload attachments.
    You can download attachments.
    You can't post HTML code.
    You can't edit HTML code.
    You can't post IFCode.
    You can't post JavaScript.
    You can post emoticons.
    You can't post or upload images.

    Select a forum

































































































































































    SQLServerCentral


    Search