zoukankan      html  css  js  c++  java
  • sql 临时表循环更新月租金

     
    declare @pt_Year int
    declare @pt_Month int
    declare @pt_Day int
    declare @pt_MonthCount int
    --得到二个日期的年份

    declare @pt_table table(InfoId bigint identity(1,1),ContractCode varchar(100),RendStartDate datetime ,RendEndDate datetime ,TotalAmount money)
    insert into @pt_table
    select ContractCode,RendStartDate,RendEndDate,TotalAmount from dbo.Asset_Contract


    declare @pt_year1 int
    declare @pt_year2 int
    declare @pt_month1 int
    declare @pt_month2 int
    declare @pt_day1 int
    declare @pt_day2 int
    declare @pt_Count bigint
    declare @pt_ContractCode varchar(100)
    declare @pt_RendStartDate datetime
    declare @pt_RendEndDate datetime
    declare @pt_TotalAmount money

    select @pt_Count=count(InfoId) from @pt_table

    while @pt_Count>0
    begin

    select @pt_ContractCode=ContractCode,
    @pt_RendStartDate=RendStartDate,
    @pt_TotalAmount=TotalAmount,
    @pt_RendEndDate=RendEndDate
    from @pt_table where InfoId=@pt_Count

    set @pt_year1=subString(CONVERT(varchar(100),@pt_RendStartDate, 23),1,4)
    set @pt_year2=subString(CONVERT(varchar(100),@pt_RendEndDate, 23),1,4)

    set @pt_month1=subString(CONVERT(varchar(100),@pt_RendStartDate, 23),6,2)

    set @pt_month2=subString(CONVERT(varchar(100),@pt_RendEndDate, 23),6,2)

    set @pt_day1=subString(CONVERT(varchar(100),@pt_RendStartDate , 23),9,2)

    set @pt_day2=subString(CONVERT(varchar(100),@pt_RendEndDate , 23),9,2)


    set @pt_MonthCount=(@pt_year2-@pt_year1)*12+(@pt_month2-@pt_month1)
    set @pt_Day=@pt_day2-@pt_day1
    if(@pt_Day>0)
    begin
    set @pt_MonthCount=@pt_MonthCount+1
    end
    if(@pt_Day<0)
    begin
    set @pt_MonthCount=@pt_MonthCount-1
    end

    update Asset_Contract
    set YueRental=TotalAmount/@pt_MonthCount
    where IsChecked=1

    set @pt_Count=@pt_Count-1
    end


    --if (day > 0) {
    --                len += 1;
    --            }
    --            else if (day < 0)
    --            {
    --                len -= 1;
    --            }
    --update Asset_Contract
    --set YueRental=TotalAmount/(datediff(month,RendStartDate,RendEndDate))
    --where IsChecked=1
    --
    --
    --
    --select datediff(month,RendStartDate,RendEndDate) from Asset_Contract where ContractCode='NTJQ201200303'
    --
    --
    --select datediff(month,'2012-01-01','2012-05-01')+1


    --
    --select datediff(year,'2012-9-1','2013-8-31')
    ----得到二个日期的月份
    --select datediff(month,'2012-9-1','2013-8-31')
    --
    ----得到二个日期的天数
    --select datediff(day,'2012-9-1','2013-8-31')
    --
    --declare @pt_year1 int
    --declare @pt_year2 int
    --declare @pt_month1 int
    --declare @pt_month2 int
    --declare @pt_day1 int
    --declare @pt_day2 int


     

  • 相关阅读:
    vue / js使用video获取视频时长
    vuex的使用
    eclipse中没有tomcat小猫
    Windows下安装Redis服务
    postman上传文件对参数的contentType类型设置方式
    !与&&优先级的问题
    备份数据库中的某个表的数据报错Statement violates GTID consistency
    Error 'Cannot add or update a child row: a foreign key constraint fails故障解决
    解除项目与其他服务的强依赖
    常见的几种异常类型 Exception
  • 原文地址:https://www.cnblogs.com/howie/p/2818071.html
Copyright © 2011-2022 走看看