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


SSIS - Using a server variable within execute sql task code


SSIS - Using a server variable within execute sql task code

Author
Message
Linda Hale-470385
Linda Hale-470385
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 114
Hi guys,

I have created a user defined variable at package level called DestinationServer, the value of which I change depending on which server I am using. I have used this in an expression for a connection manager and it works as expected.

What I am now trying to do is reference this variable in an execute sql task i.e

Insert into Table1(column1, column2)
Select (blah1, blah2)
from [user:BigGrinestinationServer].databasename.dbo.tablename
where .......

I have tried creating a parameterMapping and referenicng that:-
Insert into blahblablah(column1, column2)
Select (blah1, blah2)
from ?.databasename.dbo.tablename


but I cant seem to get it to work? Is this achievable within SSIS?

TIA

Newbie
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27175 Visits: 13268
You cannot use the variable like that directly. (e.g. placing a ? instead of a servername is not an option).
You need to use dynamic SQL. Create a string that contains your SQL statement with a placeholder for the servername.
Create a variable and map this variable to your parameter (using the question mark).
Then, use the REPLACE function on the string to replace the placeholder with the variable value. You'll have your final SQL statement stored in that string variable.

Execute this SQL statement with EXEC or with sp_executesql.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8559 Visits: 7660
The other option is using the expressions in the OLEDB source object, and setting servername via the variable, then using that as the source for a dataflow pair.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27175 Visits: 13268
Craig Farrell (11/22/2010)
The other option is using the expressions in the OLEDB source object, and setting servername via the variable, then using that as the source for a dataflow pair.


Unfortunately the question was about an Execute SQL Task ;-)


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8559 Visits: 7660
da-zero (11/22/2010)
Craig Farrell (11/22/2010)
The other option is using the expressions in the OLEDB source object, and setting servername via the variable, then using that as the source for a dataflow pair.


Unfortunately the question was about an Execute SQL Task ;-)


Agreed, but since that task was:

Insert into blahblablah(column1, column2)
Select (blah1, blah2)
from ?.databasename.dbo.tablename



There are now some other approaches. :-)

The task she's trying to do can also be done from the data flow objects, and would allow her to use it the way she's trying to, just with a slightly different approach.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27175 Visits: 13268
You do make some good valid points, but if the real requirement is to make it all dynamic, then there is a chance that the data flow is not an option. I guess it is up to the original poster to decide :-)


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Chris Hurlbut
Chris Hurlbut
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1850 Visits: 540
My vote is with Da-Zero. Dynamic SQL in Execute SQL tasks. I think these run more efficiently than Data Flow tasks, especially if you will be working with big record sets.
grahamc
grahamc
Right there with Babe
Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)Right there with Babe (730 reputation)

Group: General Forum Members
Points: 730 Visits: 1039
churlbut (11/23/2010)
My vote is with Da-Zero. Dynamic SQL in Execute SQL tasks. I think these run more efficiently than Data Flow tasks, especially if you will be working with big record sets.


Why is Dynamic SQL in Execute SQL tasks more efficient with big record sets??

Im with Craig...
Chris Hurlbut
Chris Hurlbut
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1850 Visits: 540
The Execute SQL task is better in my experience than Data Flow tasks, dynamic sql is just a method for achieving what he wants to do in the Execute SQL task. Again, this is just my observation.
Linda Hale-470385
Linda Hale-470385
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 114
Thanks for all your replies, I'm going to give Da Zeros suggestion a try, will let you know the results.
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