首先建立一個分隔字符串函數:sql
create function SplitIn(@c varchar(2000),@split varchar(2)) returns @t table(col varchar(20)) as begin while(charindex(@split,@c)<>0) begin insert @t(col) values (substring(@c,1,charindex(@split,@c)-1)) set @c = stuff(@c,1,charindex(@split,@c),'') end insert @t(col) values (@c) return end
第二,在存儲過程當中使用SplitIn函數函數
select @TeamRoomCount = COUNT(Work_GuestAccount.AccountID) FROM Work_GuestAccount INNER JOIN Work_GuestEntryIndex ON Work_GuestAccount.EntryID = Work_GuestEntryIndex.EntryID WHERE (Work_GuestEntryIndex.TeamID <> '') AND (Work_GuestEntryIndex.TeamID IS NOT NULL) AND (Work_GuestAccount.RoomID IN (select col from SplitIn(@TeamRoomNo,',')))