zoukankan      html  css  js  c++  java
  • SQl函数的写法

    USE [ChangHong_612]
    GO
    /****** Object: UserDefinedFunction [dbo].[FN_GetProdQty] Script Date: 10/08/2015 14:05:16 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    /***********************************************
    -- Author: lxf
    -- Create date: 2012-12-08 10:25:00
    -- Description: 求产品数
    -- Module: Public
    select dbo.FN_GetProdQty('2013112007','20131120070002','(CKZ1110-002)','P01',1.01,'2013-09-13 00:00','2013-09-13 23:59')
    ***********************************************/
    ALTER FUNCTION [dbo].[FN_GetProdQty]
    (
    @MO varchar(20),
    @DispatchNO varchar(50),
    @ItemNO varchar(20),
    @ProcCode varchar(20) = '',
    @DispatchPrior float ,
    @StartDate varchar(20),
    @EndDate varchar(20)
    )
    RETURNS int
    AS
    BEGIN
    DECLARE @OpenMouldNum int, --开模数
    @StockNum int,
    @RETURN int,
    @charType char(1) --机器类型,1表示注塑机,2表示植毛机
    set @OpenMouldNum= 0
    select @charType=isnull(m.MachineClsId,'1')
    from V_DispatchOrder d
    join mes_machine m on d.MachineNO=m.MachineNO
    where MO = @MO
    and DispatchNO = @DispatchNO
    and (@DispatchPrior = 0 or DispatchPrior = @DispatchPrior)
    and ItemNo = @ItemNO
    and (@ProcCode = '' or ProcCode = @ProcCode)
    --结束时间大于当前时间,自动截断
    if(@EndDate <> '')
    begin
    if(getdate()<convert(datetime,@EndDate))
    begin
    set @EndDate = convert(varchar(19),getdate(),121)
    end
    end


    select @OpenMouldNum = dbo.FN_GetProdMoNum(@MO, @DispatchNO, @DispatchPrior, @StartDate, @EndDate, @ProcCode)

    IF (@charType = '1')
    BEGIN
    SELECT @RETURN=@OpenMouldNum * isnull(do.SocketNum,0) --注塑机的数量等于开模数*模穴数
    FROM V_DispatchOrder DO
    WHERE DO.MO = @MO and DO.DispatchNO = @DispatchNO
    and (@ProcCode = '' or ProcCode = @ProcCode)
    and (@DispatchPrior = 0 or DispatchPrior = @DispatchPrior)
    and DO.ItemNO = @ItemNO
    END
    ELSE IF (@charType = '2')
    BEGIN
    SELECT @RETURN=isnull(CAST(PM.FieldValue as int),0)*isnull(cast(PP.FieldValue as int),0)*@OpenMouldNum
    FROM V_DispatchOrder DO
    left JOIN dbo.MES_MachineExt PM ON DO.MachineNo = PM.MachineNo
    left JOIN dbo.MES_ItemExt PP ON DO.ItemNO = PP.ItemNO
    WHERE DO.MO = @MO and DO.DispatchNO =@DispatchNO
    and (@ProcCode = '' or ProcCode = @ProcCode)
    and (@DispatchPrior = 0 or DispatchPrior = @DispatchPrior)
    and DO.ItemNO= @ItemNO
    and PM.FieldName='GroupNum' and PP.FieldName='OutNum'
    END
    Else
    BEGIN
    SELECT @RETURN = @OpenMouldNum
    END
    if @RETURN is null set @RETURN=0
    if @RETURN<=0 set @RETURN=0
    RETURN @RETURN
    END

    ------------------------------------------

    USE [ChangHong_612]
    GO
    /****** Object: UserDefinedFunction [dbo].[FN_GetAdjustMachineQty] Script Date: 10/08/2015 14:50:05 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    /***********************************************
    --Author: lxf
    --Create date:2012.12.14
    --select dbo.FN_GetAdjustMachineQty('2014082203','20140822030001','1201154',1.01,'','','','P01')
    --Description:取得调机时的产品数。
    ***********************************************/

    ALTER function [dbo].[FN_GetAdjustMachineQty]
    (
    @MO varchar(100),
    @DispatchNO varchar(100),
    @ItemNO varchar(50),
    @DispatchPrior float,
    @BeginDate varchar(20),
    @EndDate varchar(20),
    @ClassType varchar(20) = '',
    @ProcCode varchar(20) = ''
    )
    returns int
    as
    begin
    declare @GoodQty int,@BadQty int
    set @GoodQty=0
    set @BadQty=0

    if @BeginDate=''
    set @BeginDate='1900-01-01 00:00:00'

    if @EndDate=''
    set @EndDate=GETDATE()

    select @GoodQty = sum(isnull(GoodQty,0))
    from(
    select DispatchNo, ItemNO, GoodQty,
    (select name from
    (select Name,BeginTime,
    case
    when BeginTime>EndTime then Cast(CAST(SUBSTRING(endtime,1,2) as int)+24 as varchar)+':00'
    else EndTime end
    EndTime
    from MES_BCDetail
    )
    b
    where BeginTime<convert(char(5),a.StartDate,108) and EndTime >convert(char(5),a.StartDate,108)
    )
    as ClassType
    from MES_AdjustMachine a
    where DispatchNO = @DispatchNO and ItemNO = @ItemNO
    and (@DispatchPrior = 0 or DispatchPrior = @DispatchPrior)
    and (@ProcCode = '' or ProcCode = @ProcCode)
    and (StartDate IS NOT NULL AND EndDate IS NOT NULL)
    and (EndDate>=@BeginDate AND StartDate<=@EndDate)
    ) a
    where @ClassType = '' or ClassType = @ClassType

    select @BadQty = sum(isnull(BadQty,0))
    from(
    select DispatchNo, ItemNO, BadQty,
    (select name from (select Name,BeginTime,case when BeginTime>EndTime then Cast(CAST(SUBSTRING(endtime,1,2) as int)+24 as varchar)+':00'
    else EndTime end EndTime from MES_BCDetail ) b where BeginTime<convert(char(5),a.QcDate,108) and EndTime >convert(char(5),a.QcDate,108)) as ClassType
    from MES_Qc a
    where qctype in(44,36,25,26,24,47) and DispatchNO = @DispatchNO and ItemNO = @ItemNO
    and (@DispatchPrior = 0 or DispatchPrior = @DispatchPrior)
    and (@ProcCode = '' or ProcCode = @ProcCode)
    and (QCDate>=@BeginDate AND QCDate<=@EndDate)
    ) a
    where @ClassType = '' or ClassType = @ClassType

    return isnull(@GoodQty,0)+isnull(@BadQty,0)
    end

  • 相关阅读:
    Ubuntu 找不到ARM64 的源
    解决ubuntu下error occurred during the signature verification
    Ubuntu 16.04LTS 更新清华源
    Optimizing Deep Learning Computation Graphs with TensorRT
    Runtime Integration with TensorRT
    Python之爬虫
    缓存,队列(Redis,RabbitMQ)
    Web框架之Tornado
    Django之ORM性能优化
    Django进阶(补充)
  • 原文地址:https://www.cnblogs.com/chengjun/p/4867736.html
Copyright © 2011-2022 走看看