Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

c# - How to pass multiple value in one parameter in one variable to OLEDB Source of SSIS

I am trying to pass multiple values as one parameter. e.g: I need to get the employeeID in (1,2,3,4,5), employeeID field data type is Integer.

Query:

select * from emp where employeeID = ?

I Create a variable 'v_employeeID' with the value 1 and passed the parameter mapping in OLEDB Source. I can pass one value but I cant pass multiple value.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One workaround is to create a function:

CREATE FUNCTION [dbo].[fn_GenerateIDs]
(
 @psCSString VARCHAR(8000)
)
RETURNS @otTemp TABLE(sID VARCHAR(20))
AS
BEGIN
 DECLARE @sTemp VARCHAR(10)

 WHILE LEN(@psCSString) > 0
 BEGIN
  SET @sTemp = LEFT(@psCSString, ISNULL(NULLIF(CHARINDEX(',', @psCSString) - 1, -1),
                    LEN(@psCSString)))
  SET @psCSString = SUBSTRING(@psCSString,ISNULL(NULLIF(CHARINDEX(',', @psCSString), 0),
                               LEN(@psCSString)) + 1, LEN(@psCSString))
  INSERT INTO @otTemp VALUES (@sTemp)
 END

RETURN
END

Which will return you the table of IDs when passed in format : '1,2,3,4'

Such as :

select *
from fn_GenerateIDs('1,2,3')

Results:

+----+
| ID |
+----+
| 1  |
+----+
| 2  |
+----+
| 3  |
+----+

And then you would have to pass parameter in varchar format : "1,2,3,4,5" and compare it with the Employee table as :

    select * from emp where    
    EXISTS ( select 1 from fn_GenerateIDs('1,2,3') where employeeID = sID)

Another option if you are using SQL Server 2016 is to use a newly added function STRING_SPLIT

Which require 2 parameters: String of ids, and a separator

Such as:

select * from dbo.emp
inner join string_split('1,2,3,4',',') on dbo.emp.employeeID = value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...