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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…