--拆分字符串之后匹配结果集合
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)