取值比较数据的最低值

Alter procedure historyminxsprice
@billno varchar(30)
as
begin
        declare @custno varchar(30),@typesprc varchar(300),@zkprice numeric(18,2)
        declare @typeno varchar(30),@dislv int,@chklongdate date ,@ddate date
        set @custno=(select custno from sa_baoj with(nolock) where billno=@billno)
        select typesprc,zkprice,typeno1
        into #minxsprice_tmp1
        from sa_baojlist with(nolock) where billno=@billno
        declare sc_historyminxsprice cursor for
        select * from #minxsprice_tmp1
        open sc_historyminxsprice
        fetch next from sc_historyminxsprice into @typesprc,@zkprice,@typeno
        while @@fetch_status=0
        begin
                    set @dislv=null set @chklongdate=null set @ddate=null
                    --set @typeno=(select typeno from ba_cpinfo with(nolock) where sprc=@typesprc)
                    set @dislv=(select dislv from pd_fadiscountlvlist1 with(nolock) where typeno3='A01.01.01' and custno=@custno)
                    set @chklongdate=(select chklongdate from pd_fadiscountlvlist1 with(nolock) where typeno3=@typeno and custno=@custno)
                    set @ddate=dateadd(YY,-datediff(dd,getdate(),@chklongdate)/365,@chklongdate)--周期天数
                          declare @price1 numeric(18,2) -- 报价单最低金额
                          ,@price2 numeric(18,2)-- 订单单最低金额
                          ,@price3 numeric(18,2)-- 订单单最低金额
                          ,@price4 numeric(18,2)-- 订单单最低金额
                          ,@ddate1 date-- 基准价日期
                          ,@ddate2 date-- 基准价日期
                          set @price1=null set @price2=null  set @price3=null set @price4=null  set @ddate1=null  set @ddate2=null
                          select top 1 @price1=b.price,@ddate1=a.chkerdate from  sa_askprice a with(nolock)
                          left join sa_askpricelist b with(nolock) on a.askno=b.askno where a.Chkerdate between @chklongdate and getdate() and a.yjflg=3 and b.sprc=@typesprc
                          and a.custno=@custno
                          order by a.chkerdate desc
                          select @price2=min(a.saleps) from Sa_OrderList a with(nolock)
                          left join Sa_Order b with(nolock) on a.orderno=b.orderno
                          left outer join ba_cpinfo cp with(nolock) on a.mcode=cp.mcode
                          where cp.sprc=@typesprc and b.custno=@custno and b.chkerdate between @ddate1 and getdate() and isnull(b.billflg,0)<>3
                          --select top 1  @price3=b.price,@ddate2=a.chkerdate from  sa_askprice a with(nolock)
                          --left join sa_askpricelist b with(nolock) on a.askno=b.askno where  a.yjflg=3 and b.sprc=@typesprc
                          --and a.custno=@custno and a.chkerdate between @chklongdate and getdate()
                          --order by a.chkerdate desc
                          select @price4=min(a.saleps) from Sa_OrderList a with(nolock)
                          left join Sa_Order b with(nolock) on a.orderno=b.orderno 
                          left outer join ba_cpinfo cp with(nolock) on a.mcode=cp.mcode
                          where cp.sprc=@typesprc and b.custno=@custno and b.chkerdate between @chklongdate and getdate() and isnull(b.billflg,0)<>3
                          if cast(@price1 as varchar)='' 
                          set @price1=null
                          if cast(@price2 as varchar)='' 
                          set @price2=null
                          --if cast(@price3 as varchar)='' 
                          --set @price3=null
                          if cast(@price4 as varchar)='' 
                          set @price4=null
                    --B+:5,C+:6
                    if @dislv=5 or @dislv=6
                    begin
                                        if @price1 is null
                                        begin 
                                                update a set a.minprice=isnull(@price4,a.zkprice) from  sa_baojlist a where a.billno=@billno and a.typesprc=@typesprc 
                                                update a set a.gdjzj=null from  sa_baojlist a where a.billno=@billno and a.typesprc=@typesprc
                                        end 
                                        if @price1 is not null 
                                        begin
                                                if @price1>@price2
                                                update a set a.minprice=isnull(@price2,a.zkprice) from  sa_baojlist a where a.billno=@billno and a.typesprc=@typesprc 
                                                else
                                                update a set a.minprice=isnull(@price1,a.zkprice) from  sa_baojlist a where a.billno=@billno and a.typesprc=@typesprc
                                                update a set a.gdjzj=@price1 from  sa_baojlist a where a.billno=@billno and a.typesprc=@typesprc
                                        end 
                                        --if exists(select  * from  sa_askprice a with(nolock) left join sa_askpricelist b with(nolock) on a.askno=b.askno 
                                        --where a.Chkerdate between @chklongdate and getdate() and a.yjflg=3 and  b.sprc=@typesprc and a.custno=@custno)
                                        --begin
                                                            --if exists(select  * from  sa_askprice a with(nolock) left join sa_askpricelist b with(nolock) on a.askno=b.askno 
                                                            --where a.Chkerdate between @ddate and getdate() and a.yjflg=3 and  b.sprc=@typesprc and a.custno=@custno )
                                                            --begin
                                                            --             if @price1>@price2 
                                                            --             begin 
                                                            --                        --select @price1
                                                            --                        --select @price2
                                                            --                        update a set a.minprice=isnull(@price2,a.zkprice)
                                                            --                        from  sa_baojlist a where a.billno=@billno and a.typesprc=@typesprc
                                                            --             end
                                                            --             else
                                                            --             begin 
                                                            --                             --select 2
                                                            --                            update a set a.minprice=isnull(@price1,a.zkprice)
                                                            --                            from  sa_baojlist a where a.billno=@billno and a.typesprc=@typesprc
                                                            --                            update a set a.gdjzj=@price1
                                                            --                            from  sa_baojlist a where a.billno=@billno and a.typesprc=@typesprc
                                                            --             end
                                                            --end
                                                            --else 
                                                            --begin
                                                            --                if @price3>@price4 
                                                            --                begin
                                                            --                        update a set a.minprice=isnull(@price4,a.zkprice)
                                                            --                        from  sa_baojlist a where a.billno=@billno and a.typesprc=@typesprc
                                                            --                end
                                                            --                else
                                                            --                begin
                                                            --                        update a set a.minprice=isnull(isnull(@price3,@price4),a.zkprice)
                                                            --                        from  sa_baojlist a where a.billno=@billno and a.typesprc=@typesprc
                                                            --                        update a set a.gdjzj=@price3
                                                            --                        from  sa_baojlist a where a.billno=@billno and a.typesprc=@typesprc
                                                            --                end
                                                            --end
                                        --end
                    end
        fetch next from sc_historyminxsprice into @typesprc,@zkprice,@typeno
        end
        close sc_historyminxsprice
        deallocate sc_historyminxsprice
        drop table #minxsprice_tmp1
end

猜你喜欢

转载自blog.csdn.net/tianyustat/article/details/91788834