創建一個函數來拆分爲下面的逗號分隔值,
CREATE FUNCTION [dbo].[FnSplit]
(
@List nvarchar(2000),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
Value nvarchar(100)
)
AS
BEGIN
While (Charindex(@SplitOn,@List)>0)
Begin
Insert Into @RtnValue (value)
Select
Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))
Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
End
Insert Into @RtnValue (Value)
Select Value = ltrim(rtrim(@List))
Return
END
在你的存儲過程調用下面的函數,
CREATE PROCEDURE usp_getvalues
@var1 nvarchar(max)
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM tblEmp e
INNER JOIN tblDepartment d
on d.DeptID = e.DeptID
WHERE d.DeptID IN (SELECT value FROM [dbo].[FnSplit](@var1))
SET NOCOUNT OFF;
END
我不知道有什麼方法除了將多個值作爲單個分隔字符串傳遞並在SQL代碼中進行分解以外。 – jmcilhinney
@jmcilhinney - 還有其他的選擇,但它們都是*(imo)*也很笨重。例如,表值參數? – MatBailie