SQL 分割匹配

CREATE PROCEDURE proc_keyword
  @content  nvarchar(max)
  as
begin
declare @SplitName nvarchar(max)
declare @SubName nvarchar(max)
declare @Split nvarchar(10)
declare @CustomerWord nvarchar(max)
declare @Id int
declare @Ids  nvarchar(max)
declare @tsql  nvarchar(max)
set @CustomerWord=@content
set @Split=','
set @Ids=''
 begin
     DECLARE cur CURSOR FOR  SELECT ID,[NewName] from dbo.lampon_News
     OPEN cur
       FETCH NEXT FROM cur into @Id,@SplitName
       WHILE @@FETCH_STATUS = 0
       begin
             while(CHARINDEX(@Split,@SplitName)>0)
               begin
                       SELECT @SubName = LTRIM(RTRIM(SUBSTRING(@SplitName,1,CHARINDEX(@Split,@SplitName )-1)))
                if (CHARINDEX(@SubName,@CustomerWord)>0)
                  begin
                    set @Ids=@Ids+cast(@Id as nvarchar(max))+','

                  break
                 end
                else
                begin
                   set @SplitName=SUBSTRING(@SplitName ,CHARINDEX(@Split,@SplitName ) + 1,LEN(@SplitName))
                end
             end
             if (CHARINDEX(@SplitName,@CustomerWord)>0)
              begin
                  set @Ids=@Ids+cast(@Id as nvarchar(max))+','

              end
         FETCH NEXT FROM cur into @Id,@SplitName
       end
      Close cur
      DEALLOCATE  cur
      if (LEN(@Ids)=0)
      begin
          set @tsql=' select * from dbo.lampon_News where ID =null';
      end
     else
     begin
         set @Ids=SUBSTRING(@Ids,1,LEN(@ids)-1)
         set @tsql=' select * from dbo.lampon_News where ID in('+@Ids+')';
      end
      print @tsql
      EXEC(@tsql)
  end
end
 

execute proc_keyword '你好'

 

posted @ 2014-07-06 17:19  lampon  阅读(398)  评论(0编辑  收藏  举报