7

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)

4

3 回答 3

14

在 SQL 任务中,在General菜单下,将ResultSet属性设置为SingleRow

然后,在ResultSet菜单中,按照 select 子句的顺序添加变量,并将别名与变量映射。举个例子 :

SELECT 1 AS One, 2 AS Two

在此处输入图像描述

于 2013-09-16T13:18:14.283 回答
0

我通过将结果集映射为从零开始的序数集找到了解决方案。例如,在任务属性中的“结果集”下:

result name   |   variable name
-------------------------------
     0        |       a
     1        |       b
     3        |       c
     4        |       d

这种方法也允许我保持我的 SQL 语句不变。

于 2013-09-16T13:42:36.140 回答
-1

Create one stored procedure with four output parameters:

CREATE SP_data(
  @x INT,
  @a int OUTPUT,
  @b int OUTPUT,
  @c int OUTPUT 
  @d int output
)
AS
  SELECT top 1 
    @a = a,
    @b= b, 
    @c  = x + y,
    @d= @d  
FROM tablename
WHERE Switch = @x

in the Execute SQL Query in the parameter mapping tab create four output parameters

the execute the proc

EXEXCUT SP_data 1,? OUTPUT,? OUTPUT,? OUTPUT, ? OUTPUT
于 2013-09-16T13:21:49.253 回答