--拆分字符串之後匹配結果集合
CREATE FUNCTION [dbo].[fnSplit](
@sInputList VARCHAR(8000) -- List of delimited items
, @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(8000))
BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
BEGIN
SELECT
@sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
@sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))
IF LEN(@sItem) > 0
INSERT INTO @List SELECT @sItem
END
IF LEN(@sInputList) > 0
INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
END
----------------------使用方法
if object_id('tempdb..#Tmp') is not null
drop table #Tmp
create table #Tmp --創建臨時表#Tmp
(
ID VARCHAR(100)
);
DECLARE @iid VARCHAR(100)
declare @name varchar(500)
declare cursor1 cursor for --定義遊標cursor1
select iid,props from Iteminfos --使用遊標的對象
open cursor1 --打開遊標
fetch next from cursor1 into @iid,@name --將遊標向下移1行,獲取的數據放入之前定義的變量@iid,@name中
while @@fetch_status=0 --判斷是否成功獲取數據
begin
IF((select COUNT(*) FROM fnSplit(@name, ';') WHERE item = '20000:20090')>0)
INSERT INTO #Tmp (ID) VALUES (@iid)
fetch next from cursor1 into @iid,@name --將遊標向下移1行
end
close cursor1 --關閉遊標
deallocate cursor1
SELECT * FROM dbo.Iteminfos WHERE iid IN( SELECT ID FROM #Tmp)