Solved

How to create Counter variable in Foreach loop container SSIS 2008

Posted on 2011-02-28
4
1,377 Views
Last Modified: 2013-11-10
Hi There,

I was working on creating counter variable inside foreach loop container because, I'm having data like this

(stocknumber_randomID)
00005543_10                      
00005543_12                      
00005543_4564                  
00006644_567                    
00006644_768  

and desired output

(stocknumber_randomID)   NEW SEQUENCE
00005543_10                      1
00005543_12                      2
00005543_4564                  3
00006644_567                    1
00006644_768                    2
and so on.....

If I can use counter in foreach loop I can achieve this. Could you please suggest me how to setup incremental counter inside foreach loop

Thanks
Vamsi,
0
Comment
Question by:vepak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 26

Expert Comment

by:tigin44
ID: 35002061
you may use the ROW_NUMBER function ie...

SELECT stocknumber_randomID, ROW_NUMBER() OVER (PARTITION BY LEFT(stocknumber_randomID,CHARINDEX('_',stocknumber_randomID-1)) ORDER BY stocknumber_randomID) AS newSequence
FROM yourTable
0
 

Author Comment

by:vepak
ID: 35002314
it says
Conversion failed when converting the varchar value '00003311_6718.jpg' to data type int.

as I have file names as 00003311_6718.jpg and so on....
0
 
LVL 26

Accepted Solution

by:
tigin44 earned 500 total points
ID: 35002379
a misplaced paranthesisi  correct one is this

SELECT stocknumber_randomID, ROW_NUMBER() OVER (PARTITION BY LEFT(stocknumber_randomID,CHARINDEX('_',stocknumber_randomID)-1) ORDER BY stocknumber_randomID) AS newSequence
FROM yourTable
0
 

Author Comment

by:vepak
ID: 35002434
Thanks man it worked. Awesome.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question