Use variable in execute sql task

Hello Experts,

I created an SSIS package that uses a foreach loop container to run a series of tasks for my clients.  I have 12 clients that do the same task, so I thought it was best to use a loop so I only need one package, not 12.  I have a particular task that is used in case a different task fails.  It is an execute sql task:

declare @Storeid as varchar(50);
set @Storeid = ?;

select * into journaltemp2
           from (
select * from openrowset('MSDASQL'
               ,'Driver={Microsoft Access Text Driver (*.txt, *.csv)}; 
                   DefaultDir=\\myserver\ftps\@Storeid\'
               ,'select * from journal.csv')) as journaltemp2;
               go

Open in new window


I'm trying to use the same variable the foreach loop uses for the StoreID for the DefaultDir of the openrowset command.  I've attached some images of the setup for the foreach loop and the execute task.  I'm not sure how to reference the Storeid variable inside this execution task so I get the correct directory for each loop.  How do I do this?
executetaskpm.jpg
foreachmapping.jpg
jay-areAsked:
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.

Kyle AbrahamsSenior .Net DeveloperCommented:
select * from openrowset('MSDASQL'
               ,'Driver={Microsoft Access Text Driver (*.txt, *.csv)};
                   DefaultDir=\\myserver\ftps\' + @Storeid + '\'
               ,'select * from journal.csv')) as journaltemp2;

@storeid is a string, you have to seperate it out from your text.
0
jay-areAuthor Commented:
Hi ged325,

I changed the execute task to:

declare @Storeid as varchar(50);
set @Storeid = ?;

select * into journaltemp2
           from (
select * from openrowset('MSDASQL'
               ,'Driver={Microsoft Access Text Driver (*.txt, *.csv)};
                   DefaultDir=\\myserver\ftps\' + @Storeid + '\'
               ,'select * from journal.csv')) as journaltemp2;
               go

I get the error:
failed with the following error: "Incorrect syntax near '+'.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Does this work if you hard code it for one folder?

Did you add the parameter mapping for the storeid in the task as well for the variable to pass in?
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

jay-areAuthor Commented:
Hard coding the folder does work.

The images I attached in my original post show how the parameter mapping is done for this specific execute task.  I must have done something wrong there.  I use the storeid variable just fine in my connection managers, but can't seem to get it to work with this one execute task.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
can you post the hardcoded string?  

And is storeID an int or a varchar?

Try the following first:

select * into journaltemp2
           from (
select * from openrowset('MSDASQL'
               ,'Driver={Microsoft Access Text Driver (*.txt, *.csv)};
                   DefaultDir=\\myserver\ftps\' + cast(@Storeid as varchar(50)) + '\'
               ,'select * from journal.csv')) as journaltemp2;
               go
0
jay-areAuthor Commented:
I still get the error:  Incorrect syntax near '+'

select * into journaltemp2
           from (
select * from openrowset('MSDASQL'
               ,'Driver={Microsoft Access Text Driver (*.txt, *.csv)};
                   DefaultDir=\\myserver\ftps\' + cast(@Storeid as varchar(50)) + '\'
               ,'select * from journal.csv')) as journaltemp2;
               go

If I were to hard code the storeid it would look like:

select * into journaltemp2
           from (
select * from openrowset('MSDASQL'
               ,'Driver={Microsoft Access Text Driver (*.txt, *.csv)};
                   DefaultDir=\\myserver\ftps\1234\'
               ,'select * from journal.csv')) as journaltemp2;
               go

The Storeid variable I have setup in this package is a string.  When I map it inside the execute task I set it to varchar.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Is there a way to inspect the actual value of @storeID . . . like insert it into a table?  Make sure there are no spaces after the fact and that the directory is correctly formatted?

here's a way of doing it without the +:

select * into journaltemp2
           from (
select * from openrowset('MSDASQL'
               ,replace('Driver={Microsoft Access Text Driver (*.txt, *.csv)};
                   DefaultDir=\\myserver\ftps\{0}\', '{0}', @StoreID)
               ,'select * from journal.csv')) as journaltemp2;
               go
0

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
jay-areAuthor Commented:
select * into journaltemp2
           from (
select * from openrowset('MSDASQL'
               ,replace('Driver={Microsoft Access Text Driver (*.txt, *.csv)};
                   DefaultDir=\\myserver\ftps\{0}\','{0}', @Storeid)
               ,'select * from journal.csv')) as journaltemp2;
               go

Incorrect syntax near replace.  

There must be an issue with the storeid variable.  Should I put another execute in before this one to write out the @Storeid to a temp table or something?
0
jay-areAuthor Commented:
Ok you sent me off in the right direction.  I decided to insert the Storeid value into a new table within an execute task.  I needed to make sure I had the right syntax so I found this:

stackoverflow.com/questions/3308048/ssis-insert-variable-values-into-a-table

Wow, can't believe I didn't find this sooner.  I put the entire select statement I've been working on in the expressions tab of the execute task using "SqlStatementSource".  I had to encase it all in double quotes and escape my backslashes but it works!

Thanks for the help!
0
jay-areAuthor Commented:
The stackoverflow link helped me figure out that I should be using the expressions instead of trying this directly in the sql query.
0
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.