CREATE PROCEDURE dbo.prt_Schedule @ltQueryFrom datetime, @ltQueryTo dateTime, @tiDeptID int, @tcDeptList varchar(1000) as select * from dbo.myTable where dbo.myTable.DeptID in (select val from dbo.inlist(@tcDeptList))Function Code
CREATE FUNCTION DBO.Inlist (@list varchar(8000)) RETURNS @tbl TABLE (val int not null, Ord int not null) AS BEGIN Declare @index int, @pos int, @str varchar(8000), @num int Set @pos = 1 Set @index = 1 While @index > 0 Begin set @index = charindex(',', @list, @pos) if @index > 0 Set @str = substring(@list, @pos, @index - @pos) Else Set @str = substring(@list, @pos, Len(@list)) Set @str = ltrim(rtrim(@str)) Set @num = cast(@str as integer) Insert @tbl (val,ord) values (@num,@pos) Set @pos = @index + 1 End Return End