zoukankan      html  css  js  c++  java
  • 存储过程分割字符串查询

    create proc [dbo].[Proc_RS_VRDPAC_MRC1_SelectPassCountByYearMonthtest]     
      @month varchar(100)      
      as
    declare @m as int, @sql as nvarchar(1000),@n as int ,@id as nvarchar(100),@id1 as nvarchar(100),@num as int  
    set @m = CHARINDEX (',',@month)
    set @n = 0
    set @num=0
    set @sql='select  month(dbo.fn_PrimeObjects_GetLocalTime(a.ExamingOn)) as months,b.BU,    
    sum(CONVERT(decimal(18, 0), a.IsPassed)) as pasman,   
    count(distinct a.stuid) -sum(CONVERT(decimal(18, 0), a.IsPassed)) as nopas   
    from VK_RDPAC_MRC1_Record as a    
    join Employee as b    
    on  a.stuid=b.FID    
    where BU is not null  and bu is not null and b.ProduceLine is not null'
    WHILE  @m>0 
     BEGIN 
            set  @id=substring(@month,@n,@m-@n) 
            set  @n=@m+1 
            set  @m=CHARINDEX(',',@month,@n)
            print @m
            if @num=0
            begin
               if @m=0
               begin
                    set @sql=@sql+' and CONVERT(varchar(7),a.ExamingOn,120) in ('''+@id+''')'
               end
               else
               begin
                    set @sql=@sql+' and (CONVERT(varchar(7),a.ExamingOn,120) in ('''+@id+''')'
               end
            end
            else
            begin
               if @m=0
               begin
                    set @sql=@sql+' or CONVERT(varchar(7),a.ExamingOn,120) in ('''+@id+'''))'
               end
               else
               begin
                    set @sql=@sql+' or (CONVERT(varchar(7),a.ExamingOn,120) in ('''+@id+''')'
               end
            end
            set @num=@num+1
     END
     set @sql=@sql+' group by  month(dbo.fn_PrimeObjects_GetLocalTime(a.ExamingOn)),b.BU order by b.BU'
    exec sp_executesql @sql

  • 相关阅读:
    Python 中的map函数,filter函数,reduce函数
    编程中,static的用法详解
    C++ list容器系列功能函数详解
    python中的configparser类
    310实验室OTL问题----将写好的C++文件转换成Python文件,并将数据可视化
    310实验室OTL问题
    常量指针、指针常量、指向常量的指针常量
    Iterator迭代器的相关问题
    struts2中action中的通配符
    struts2访问servlet API
  • 原文地址:https://www.cnblogs.com/caiyun/p/3760997.html
Copyright © 2011-2022 走看看