创建一个存储过程,查询在校生

一直都是临时写语句,现在一次性建立存储过程,日后直接调用就好。

create procedure sp_zaixiao
 
as
declare @curyear int
declare @curmonth int 
declare @tolrow int
declare @strsql varchar(200)
set @curyear=year(getdate())
set @curmonth=month(getdate())
if @curmonth>7 
 
select case
        
when dwdm='4420000' then '校本部'
        
else '分校'
        
end as '单位',
            
count(xh) as '人数' from xsjbdab where xjztdm='1' and nj>(@curyear-3)  and xslbdm<>'61'
        
group by (case
        
when dwdm='4420000' then '校本部'
        
else '分校'
        
end)
else
  
select case
        
when dwdm='4420000' then '校本部'
        
else '分校' end as '单位',
         
count(xh) as '人数' from xsjbdab where xjztdm='1'   and xslbdm<>'61'
             
and( nj>(@curyear-3or (nj=(@curyear-3and zsjj='3'))
   
group by (case
        
when dwdm='4420000' then '校本部'
        
else '分校'
        
end)

原文地址:https://www.cnblogs.com/samsonleung/p/1239809.html