zoukankan      html  css  js  c++  java
  • 人才网查找职位的复杂SQL用法

    问题:职位表Job_OfficeList的GWCode保存的数据格式 24008,29001,32344

            职位订阅表保存的查询条件是 24008,31323

    要查询该条件的数据:实现方法:

    一:自定义分割函数:

       

    Create  function   [F_Split](@c varchar(4000) , @split varchar(2))  
      returns @t table(col varchar(256))  
      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  

    二:判断用逗号隔开的变量是否存在集合中

    create function   [dbo].[checkStringTostring](@datas   varchar(200),@values    varchar(200))  
      returns   bit 
      as  
         begin  
      declare @tmpcount int
      set @tmpcount=0
      declare @tmpstr varchar(50)
      
      declare mycur cursor for
      select col from dbo.f_split(@datas,',')
      open mycur

      fetch next from mycur into @tmpstr
      WHILE @@FETCH_STATUS = 0
       begin
        if len(@tmpstr)=4
         begin
         select @tmpcount=count(*) from dbo.f_split(@values,',') where substring(col,1,4)=@tmpstr
          if(@tmpcount>0)
           begin
            CLOSE mycur               
            DEALLOCATE mycur
            return 1
           end
         end
        else
         begin
         select @tmpcount=count(*) from dbo.f_split(@values,',') where col=@tmpstr
          if(@tmpcount>0)
           begin
            CLOSE mycur               
            DEALLOCATE mycur
            return 1
           end
         end
        fetch next from mycur into @tmpstr
       end

      declare mycur2 cursor for
      select col from dbo.f_split(@values,',') where len(col)=4
      open mycur2

      fetch next from mycur2 into @tmpstr
      WHILE @@FETCH_STATUS = 0
       begin
         select @tmpcount=count(*) from dbo.f_split(@datas,',') where substring(col,1,4)=@tmpstr
          if(@tmpcount>0)
           begin
            CLOSE mycur               
            DEALLOCATE mycur
            CLOSE mycur2               
            DEALLOCATE mycur2
            return 1
           end
       fetch next from mycur2 into @tmpstr
       end
      
     CLOSE mycur               
     DEALLOCATE mycur
     CLOSE mycur2               
     DEALLOCATE mycur2
         return 0;
        end  

    三。用SQL语句实现:

      select * from job_officelist where dbo.checkStringTostring(GWCode,'24008,22008')=1

  • 相关阅读:
    Dynamics AX 2012 R2 配置E-Mail模板
    Dynamics AX 2012 R2 设置E-Mail
    Dynamics AX 2012 R2 为运行失败的批处理任务设置预警
    Dynamics AX 2012 R2 耗尽用户
    Dynamics AX 2012 R2 创建一个专用的批处理服务器
    Dynamics AX 2012 R2 创建一个带有负载均衡的服务器集群
    Dynamics AX 2012 R2 安装额外的AOS
    Dynamics AX 2012 R2 将系统用户账号连接到工作人员记录
    Dynamics AX 2012 R2 从代码中调用SSRS Report
    Dynamics AX 2012 R2 IIS WebSite Unauthorized 401
  • 原文地址:https://www.cnblogs.com/ggbbeyou/p/1853885.html
Copyright © 2011-2022 走看看