Replace a Cursor to For Each loop for SSIS package

Eng Soon Cheah 81 Reputation points

Hi all,
I'm new to SSIS, currently I facing issue when create a SSIS Package by using Cursor.
Here with the Cursor I written

   use TestDB  
       DECLARE @storedprocedure_name varchar(100)  
       DECLARE @cmd VARCHAR(4000)  
       DECLARE @FilePath varchar(100)  
       DECLARE @FileNames varchar(100)  
       SELECT CommandText,FilePath,FileNames FROM dbo.MultipleQuery  
       OPEN spCursor  
       FETCH NEXT FROM spCursor INTO @storedprocedure_name ,@FilePath,@FileNames  
       WHILE @@fetch_status = 0  
        SET @cmd = 'bcp "' +  
         @storedprocedure_name + '" queryout "' + @FilePath +@FileNames +'" -c -UTF8 -T -S'+@@SERVERNAME  
       PRINT @cmd  
       EXEC master..xp_cmdshell @cmd  
        FETCH NEXT FROM spCursor INTO @storedprocedure_name,@FilePath,@FileNames  
       CLOSE spCursor  
       DEALLOCATE spCursor  

Here with my sample DB,

Any suggestion to replace the Cursor in SSIS Package?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,596 questions
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,449 questions
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,544 questions
0 comments No comments
{count} votes

Accepted answer
  1. Monalv-MSFT 5,891 Reputation points

    Hi @Eng Soon Cheah ,

    We can use Execute SQL Task to execute the sql query in ssis package.

    Hope the following link and picture will be helpful:

    Replacing a SQL Cursor with SSIS


    Best Regards,


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Hot issues in November--What can I do if my transaction log is full?
    Hot issues in November--How to convert Profiler trace into a SQL Server table?

    0 comments No comments

0 additional answers

Sort by: Most helpful