Loop through the contents of a table to use as Global Variables.


I need to import numerous excel documents into SQL server.

I have managed to populate a table with all the file locations and have created a DTS package for the import.

What i would like to do is loop through this table updating the global variable each time with the file location thus importing all the excel files.

Any pointers most appreciated. (Not used DTS that much!)



Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Here is a good example of DTS looping through multiple files (lots of ActiveX scripting): http://www.sqldts.com/246.aspx.
BaikieAuthor Commented:
Thanks for the link some good examples there. I have managed to solve this by using a cursor in a stored procedure which reads in each file location to the global variable in DTS.


declare @Date      varchar(250)
declare @Region    varchar(250)
declare @InputFile varchar(250)
declare @CMD         varchar(1000)

Set @CMD = 'DTSRun /S "(local)" /N "Get File Locations" /W "0" /E'

Execute master..xp_cmdshell @CMD

declare Region_Vars_Cursor cursor read_only for  
Select [Date],[Filename],[Region]
From reference

open Region_Vars_Cursor

Fetch next from Region_Vars_Cursor into @Date, @InputFile, @Region

While @@FETCH_STATUS = 0

set @CMD = 'DTSRun /S "(local)" /N "Import Metrics Data" /A "gblDate":"8"='+@Date+' /A "gblFileName":"8"='+@InputFile+' /A "gblRegion":"8"='+@Region+' /W "0" /E'

Execute master..xp_cmdshell @CMD

Fetch next from Region_Vars_Cursor into @Date, @InputFile, @Region

End /* End While Loop */

CLOSE Region_Vars_Cursor
DEALLOCATE Region_Vars_Cursor

Set @CMD = 'DTSRun /S "(local)" /N "MoveFiles" /W "0" /E'

Execute master..xp_cmdshell @CMD



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony PerkinsCommented:
That solution looks painful.  But whatever works for you ...
BaikieAuthor Commented:
I did say i was new to DTS!!! :o)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.