Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSIS- Set Multiple variables via a single SQL task

I'm trying to set multiple variables to the result of an SQL Query that returns a single row with multiple columns. The SQL statement is in the format of:

SELECT top 1 
    a,
    b, 
    c = x + y,
    d  
FROM tablename
WHERE aSwitch = 1

So I wish to use an 'Execute SQL Query' task which will set 4 package variables with the results of the query.

For example, if the results of the query were:

|    a    |     b   |    c    |  d  |
-------------------------------------
|   duck  |   cow   | rabbit  |  42 |

Then the state of the variables after execution would be:

var1 = duck
var2 = cow
var3 = rabbit
var4 = 42

Any ideas?

(using VS/SQL 2005)

like image 689
Jamie Stuart Robin Parsons Avatar asked Sep 16 '13 12:09

Jamie Stuart Robin Parsons


People also ask

How do you pass a variable in an execute task in SSIS?

Execute SQL Task in SSIS allows user to execute parameterized SQL statement and create mapping between these parameters and the SSIS variables. To add a parameter into a SQL statement you must use a parameter marker which differs based on the connection type. Select * from table where ID > ?

Can you assign multiple values to a variable in SQL?

Assigning multiple values to multiple variablesIf you have to populate multiple variables, instead of using separate SET statements each time consider using SELECT for populating all variables in a single statement. This can be used for populating variables directly or by selecting values from database.


1 Answers

In the SQL task, under General menu, set the ResultSet property to SingleRow.

Then, in the ResultSet menu, add the variables in the order of your select clause and map the aliases with the variables. For exemple :

SELECT 1 AS One, 2 AS Two

enter image description here

like image 161
jazzytomato Avatar answered Sep 19 '22 12:09

jazzytomato