sql server 函数,存储过程,游标以及while和if语句的嵌套使用

编写这个存储过程的主要原因是自己太懒,不愿意每天往表里面做数据,所以就写了他。不仅仅是学习,也是自己的锻炼。

USE [oaerp]
GO
/****** Object:  StoredProcedure [dbo].[s_Get_AutoYh]    Script Date: 02/11/2015 17:17:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:  
-- Create date:
-- Description: 

-- =============================================
ALTER PROCEDURE [dbo].[s_Get_AutoYh]

 @sdtm datetime=null,--开始的时间
 @edtm datetime=null,--结束的时间
 @DepoID char(10)=null,
 @UserID char(20)=null,
 @type varchar(30)=null,
 -- 影响的行数
 @num int=null output
AS
--一般药品养护记录(ybyh)
if (@type='ybyh')
BEGIN
 
      --创建一张临时表
    select a.* into #tmpyh from dbo.GetGoodsBatchListUnReal(@DepoID,'') a inner join ggoods b on a.goodsid=b.goodsid
    left join gGoodsExt c on b.goodsid =c.goodsid
    where a.amount >0
    and b.catid like '01%'
    and c.storeid !='07'
    order by a.effdate
   
   
    declare @days int --计算天数
    declare @count int  --统计要养护的数据
    declare @Idx int
    declare @n int --记录每天需要插入的数据
    declare @m int --记录执行的天数
    declare @Doccode varchar(30)
    declare @GoodsID char(10)
    declare @Amount decimal(18,6)
    declare @BatchNo varchar(40)
    declare @EffDate datetime
    declare @StockArea char(2)
    declare @Station char(2)
    declare @CheckResult char(2)
    declare @DoMethod char(2)
    declare @MakID char(10)
    declare @MakDtm datetime
 
    -- 开始和结束时间的差就是本次需要添加养护记录天数
    set @days = datediff(day,@sdtm,@edtm)
    if @days = 0
    begin
    set @days = 1
    end
    --得到总的记录数,即当前店所有的品种
    select @count = count(*) from #tmpyh
    --设置每天的记录条数,向下取整。
    set @n=floor(@count/30)
   
    set @m=1
   
    --开始事务
    --begin tran
    begin try
   
  
   
   set @makdtm =@sdtm
   while(@days>0)
   begin
    
    
     declare ybyh cursor global-- 定义一个游标
     read_only
     for select goodsid,batchno,effdate,amount from #tmpyh
     open ybyh 
    
    ----如果执行的天数在输入的时间范围内
    --while(@m<=@days)
    --begin
    --  --每执行三十天,结束一次循环。
    --  if(@m%30!=0)
    --  BEGIN
      -- 打开游标
      --给参数赋值@goodsid,@batchno,@effdate,@amount
      fetch next from ybyh into @goodsid,@batchno,@effdate,@amount 
      --执行游标第一条记录
      --=fetch next from ybyh
      
      exec [s_Get_DocCodeSerial_ByTradeID] @TradeID='60',@depoID=@DepoID,@DocCode=@DocCode output
      while (@@fetch_status = 0)
      BEGIN
      --执行每天需要插入的数据,当@n小于零的时候跳出循环
      --while(@n>0)
      --begin
      
        --if not exists(select * from zGspyhRecord where goodsid =@goodsid and isnull(batchno,'')=isnull(@batchno,'') and isnull(effdate,'') =isnull(@effdate,''))
        --begin
         
         select @stockarea = case a.otcid 
         when '01' then '00'
         when '02' then '01'
         when '03' then '02'
         end 
         from ggoods a where goodsid =@goodsid
         select @Idx=isnull(max(Idx),0)+1 from zGspyhRecord where DocCode=@DocCode
        
         --设置时间和养护员信息
         set @MakID=@UserID
         --select DATEADD(dd,5550,'2015-02-12 11:11:13')
         insert into zGspyhRecord(DocCode, DepoID, Idx, GoodsID, Amount,BatchNo, EffDate, StockArea, Station, CheckResult, DoMethod, MakID, MakDtm, CheID, CheDtm)
         values(@DocCode,@DepoID,@Idx,@GoodsID,@Amount,@BatchNo,@EffDate,@stockarea,'00','00','03',@MakID,@MakDtm,null,null)

          set @n=@n-1
          --在秒上面加上idx序号,作为新的时间的秒
          set @Idx=@Idx+1
          set @MakDtm=DATEADD(SS,@Idx,@MakDtm)
          
         print('today had '+str(@n) )
         if @n<=0
         begin
          exec [s_Get_DocCodeSerial_ByTradeID] @TradeID='60',@depoID=@DepoID,@DocCode=@DocCode output
          set @MakDtm=DATEADD(DD,1,@MakDtm)
          set @days =@days -1
          set @n=floor(@count/30)
          
          if(@days%30=0)
          begin
           print('days:'+str(@days))
           break
          end
         end
        --end
        -- 执行游标,获得数据,直到读取所有的数据集合
        fetch next from ybyh into @goodsid,@batchno,@effdate,@amount
       
     --end
     --END
     
     --set @m=@m+1  --设置天数增加1天
     --set @MakDtm=DATEADD(DD,1,@MakDtm)
     END
     
     close ybyh -- 关闭游标
     deallocate ybyh -- 删除游标
 
   end

  --返回执行的记录,如果大于0,表示执行成功
    set @num =@@rowcount
    select @num
   --commit tran
   --结束事务
   end try 
    
   begin catch
    print error_message()
    --rollback tran
   end catch
END
  

你可能感兴趣的:(数据库,select,function)