zoukankan      html  css  js  c++  java
  • 分享一个自动生成单据的存储过程

          近期接到一个需求需要将一个大的单据拆分成多个小单据,这几个小单据主信息要相同,除了单号和ID外其他都要一样,于是

    我考虑了借用游标和循环来进行拆分数据,并最终实现了该方法,通过了测试,代码如下:

           IF OBJECT_ID(N'tempdb..#tmpOrgList', 'U') IS NOT NULL  
      drop table #tmpOrgList 


    ---获取所有有客户的分公司的列表
    select distinct OrgID into #tmpOrgList from v_WS400TelSurveyCustDetl 

    --声明新建单据的ID和BillNo
    declare @intBillID int=0 
    declare @chvBillNO nvarchar(20)

    ---季度首月的第一天
    declare @dtLastQtStart datetime 
    ---季度的年和季度
    declare @intYearLastQt int
    declare @intLastQt int

    ---自动执行时赋值当前年度和当前季度
    set @dtLastQtStart=DATEADD(qq, DATEDiff(qq,1,GETDATE()), 0)
    set @intYearLastQt=Year(@dtLastQtStart)
    set @intLastQt=DATEPART(QQ,@dtLastQtStart)

    ---手动执行时重新赋值指定的年度和季度
    if @Flage=1 
    begin
    set @intYearLastQt=@intYear
    set @intLastQt=@intQt
    end

    ---获取所有的抽查明细列表,去除重复的数据
    IF OBJECT_ID(N'tempdb..#tmpSVDetlList', 'U') IS NOT NULL  
     drop table #tmpSVDetlList 

    select distinct 
    [OrgID],[CompanyID],[DistributorID],[CompBranchID],[CustID],[CustChannelID]
    ,[CustName],isnull([CustContact],'') as Contacts,isnull([MobilePhone],'') as MobilePhone,
    isnull([CustTel],'') as ContactPhone,
    [CustAddr] as ClientAddress,[AreaID],[AreaType],[AreaName]
    ,[IsShow],GETDATE() as CreateTime,2 as BillerID,GETDATE() as ModifyTime 
    into #tmpSVDetlList
    from v_WS400TelSurveyCustDetl 

    declare @intOrgSVDetlCount int
    declare @intOrgSVDetlPage int 
    declare @intPageCount int
    declare @intPage int=0 

    ---每个分公司明细超过500条就开始拆分
    set @intPageCount=500

    DECLARE WS400_Cursor Cursor Local For
    SELECT OrgID FROM #tmpOrgList
    OPEN WS400_Cursor
    DECLARE @intOrgID int
    FETCH NEXT FROM WS400_Cursor INTO @intOrgID

    WHILE @@FETCH_STATUS=0 
    BEGIN
    BEGIN TRY

    ---判断所属分公司数据量是否超过了500条,超过了则拆分成几条数据来插入
    ---先生成单个分公司的抽查数据并插入临时表中
    IF OBJECT_ID(N'tempdb..#tmpOrgSVDetlList', 'U') IS NOT NULL  
    drop table #tmpOrgSVDetlList 
     
    select 
    @intBillID as BillID,ROW_NUMBER() over (order by CompanyID asc,CustID asc) as SNO,
    [OrgID],[CompanyID],[DistributorID],[CompBranchID],[CustID],[CustChannelID]
    ,[CustName],[Contacts],[MobilePhone],[ContactPhone],
    [ClientAddress],[AreaID],[AreaType],[AreaName]
    ,[IsShow],GETDATE() as CreateTime,2 as BillerID,GETDATE() as ModifyTime 
    into #tmpOrgSVDetlList 
    from #tmpSVDetlList 
    where OrgID=@intOrgID 

    ---获取当前这个分公司的所有抽查数据的数量
    select @intOrgSVDetlCount=COUNT(1) from #tmpOrgSVDetlList
    set  @intOrgSVDetlPage=@intOrgSVDetlCount/@intPageCount 
    set @intPage=0 
    ---分批循环插入
    while @intPage<=@intOrgSVDetlPage 
    begin
    ----如果明细数量刚好是分页整数倍则最后一次拆分不执行
    if @intPage*@intPageCount=@intOrgSVDetlCount 
    begin
    continue
    end
    ---获取ID
    exec dbo.prGetIdentityNoTrans 'WS400TelSurvey',@intBillID output 
    ---获取BillNO
    exec dbo.prGetBillNo 40001,-1,-1,-1,@chvBillNO output 
    ---插入主表
    insert into WS400TelSurvey
    (
    BillID,
    BillNO,
    OrgID,
    SVYear,
    SVquarter,
    CreateTime,
    BillerID,
    ModifyTime,
    Remark
    )
    select @intBillID,@chvBillNO,@intOrgID,@intYearLastQt,@intLastQt,GETDATE(),2,GETDATE(),''
    --插入明细表
    insert into WS400TelSurveyDetl(
    BillID,
    SNO,
    OrgID,
    CompanyID,
    DistributorID,
    CompBranchID,
    CustID,
    CustChannelID,
    CustName,
    Contacts,
    MobilePhone,
    ContactPhone,
    ClientAddress,
    AreaID,
    AreaType,
    AreaName,
    IsShow,
    CreateTime,
    BillerID,
    ModifyTime
    )
    select 
    @intBillID as [BillID],[SNO],
    [OrgID],[CompanyID],[DistributorID],[CompBranchID],[CustID],[CustChannelID]
    ,[CustName],[Contacts],[MobilePhone],[ContactPhone],
    [ClientAddress],[AreaID],[AreaType],[AreaName]
    ,[IsShow],GETDATE() as CreateTime,2 as BillerID,GETDATE() as ModifyTime
    from #tmpOrgSVDetlList  
    where OrgID=@intOrgID and SNO<=(@intPage+1)*@intPageCount 
    and SNO>@intPageCount*@intPage

    set @intPage=@intPage+1
    end

    IF OBJECT_ID(N'tempdb..#tmpOrgSVDetlList', 'U') IS NOT NULL  
    drop table #tmpOrgSVDetlList 

    END TRY
    BEGIN CATCH
    print 'error'
    END CATCH
    FETCH NEXT FROM WS400_Cursor INTO @intOrgID 
    END

    CLOSE WS400_Cursor 
    DEALLOCATE WS400_Cursor 

    IF OBJECT_ID(N'tempdb..#tmpOrgList', 'U') IS NOT NULL  
    drop table #tmpOrgList 
     
    IF OBJECT_ID(N'tempdb..#tmpSVDetlList', 'U') IS NOT NULL  
    drop table #tmpSVDetlList 


  • 相关阅读:
    基于jquery自己写滑动门(通用版)
    这一年,做为asp.net程序员我合格吗?
    基于jquery的滚动条滚动固定div(附Demo)
    asp.net获取数据随机显示(原创)
    为昨天一篇博文【asp.net,对于一个有点经验的猴子,我的要求高么?】做点解释
    2012年总结,2013年更精彩。
    放大镜
    be strong
    模拟凡客导航
    Ajax中Get请求与Post请求的区别(转载)
  • 原文地址:https://www.cnblogs.com/kevinGao/p/3702273.html
Copyright © 2011-2022 走看看