sql 调用函数的方法

USE [ChangHong_612]
GO
/****** Object: StoredProcedure [dbo].[st_MES_RptInspectWeight] Script Date: 09/10/2015 18:02:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER proc [dbo].[st_MES_RptInspectWeight]
(
@ItemNo varchar(50) ='860027141' --产品编号
,@ItemName varchar(50) ='' --产品描述
,@BeginTime VARCHAR(19)='2014-11-07 08:30' --计划开始时间
,@EndTime varchar(19)='2014-11-12 20:30' --计划结束时间
,@MachineNo varchar(20)=''
)
/*************
creator: zhuss
create date: 2014.09.25
update by fsq 2014.09.29 修改偏差、标准周期、只显示首检记录
Remark: 查询品质检测重量记录
exec st_MES_RptInspectWeight '850130560','','2013-06-28 15:00:00','2015-08-28 22:50:12',''
**************/
as
begin

--查询重量记录
select i.ItemName,r.BillNO,r.InspectDate,r.ItemNo,r.ThisSign,r.MothSign,r.FixWeight,
case when (r.PartWeight=0 or r.PartWeight is null) then '-' --else
--CONVERT(varchar(10),(isnull(r.Weight,0)+ isnull(r.Weight2,0)+isnull(r.Weight3,0)+isnull(r.Weight4,0)+isnull(r.Weight5,0))/
--case when r.Weight is null and r.Weight2 is null and r.Weight3 is null and r.Weight4 is null and r.Weight5 is null then r.PartWeight
--else
-- (case when r.Weight IS not null then 1 else 0 end+case when r.Weight2 IS not null then 1 else 0 end+case when r.Weight3 IS not null then 1 else 0 end
--+case when r.Weight4 IS not null then 1 else 0 end+case when r.Weight5 IS not null then 1 else 0 end) end) end PartWeight
when r.Weight is null and r.Weight2 is null and r.Weight3 is null and r.Weight4 is null and r.Weight5 is null then r.PartWeight
else CONVERT(varchar(10),(isnull(r.Weight,0)+ isnull(r.Weight2,0)+isnull(r.Weight3,0)+isnull(r.Weight4,0)+isnull(r.Weight5,0))/
case
(
case when r.Weight IS not null and r.Weight<>0 then 1 else 0 end+
case when r.Weight2 IS not null and r.Weight2<>0 then 1 else 0 end+
case when r.Weight3 IS not null and r.Weight3<>0 then 1 else 0 end+
case when r.Weight4 IS not null and r.Weight4<>0 then 1 else 0 end+
case when r.Weight5 IS not null and r.Weight5<>0 then 1 else 0 end
)
when 0 then 1 else
(
case when r.Weight IS not null and r.Weight<>0 then 1 else 0 end+
case when r.Weight2 IS not null and r.Weight2<>0 then 1 else 0 end+
case when r.Weight3 IS not null and r.Weight3<>0 then 1 else 0 end+
case when r.Weight4 IS not null and r.Weight4<>0 then 1 else 0 end+
case when r.Weight5 IS not null and r.Weight5<>0 then 1 else 0 end
)
end

) end PartWeight

,case when (r.GateWeight=0 or r.GateWeight is null) then '-' else CONVERT(varchar(10),r.GateWeight) end GateWeight
,r.ShapeCycle,dbo.FN_GetEmpNameByID(r.InspectMan) InspectMan,
MachineNo,InspectDate2=CONVERT(varchar(19),r.InspectDate,120),
--InjectionCycle=i.ItemCycle,
i.MouldNO
,InjectionCycle=(select max(InjectionCycle) from MES_MouldDetail m join FN_SplitSTR(i.MouldNO,',') b on b.Col=m.MouldNO),
MaterialInfo=[dbo].[FN_GetMaterialInfo](s.MachineNo,s.DispatchNo,s.DispatchPrior,r.InspectDate,1),
ItemNetWeight=CONVERT(varchar(10),isnull(i.NetWeight,0)-abs(isnull(i.NetWeightDown,0)))+'~'+CONVERT(varchar(10),isnull(i.NetWeight,0)+abs(isnull(i.NetWeightUp,0))),
ErrWeight=case when isnull(r.PartWeight,0)<isnull(i.NetWeight,0)-abs(isnull(i.NetWeightDown,0))
then CONVERT(varchar(20),isnull(r.PartWeight,0)-(isnull(i.NetWeight,0)-abs(isnull(i.NetWeightDown,0))))
when isnull(i.NetWeight,0)-abs(ISNULL(i.NetWeightDown,0))<=isnull(r.PartWeight,0)
and isnull(r.PartWeight,0)<=isnull(i.NetWeight,0)+abs(ISNULL(i.NetWeightUp,0))
then '/'
else '+'+convert(varchar(20),isnull(r.PartWeight,0)-(isnull(i.NetWeight,0)+abs(isnull(i.NetWeightUp,0)))) end
from MES_InspectWeightRec r
join MES_Item i on i.ItemNO=r.ItemNo
left join MES_Inspect s on r.BillNO=s.BillNO
where not ((partweight=0 and gateweight=0) or (partweight is null and gateweight is null))
--and s.CheckType=2
--update by zhuss 2014-10-21
and s.CheckType in(1,2,3,4)
and (@ItemNO='' or r.ItemNO like '%'+@ItemNO+'%')
and (@ItemName='' or i.ItemName like '%'+@ItemName+'%')
and (@BeginTime='' or CONVERT(varchar(19),r.InspectDate,120)>=@BeginTime)
and (@EndTime='' or CONVERT(varchar(19),r.InspectDate,120)<=@EndTime)
and (@MachineNo='' or s.MachineNo like '%'+@MachineNo+'%')
order by r.InspectDate desc
end

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

USE [ChangHong_612]
GO
/****** Object: UserDefinedFunction [dbo].[FN_GetMaterialInfo] Script Date: 09/10/2015 18:06:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*************
creator: zhuss
create date: 2014.09.25
Module: Public
Remark: 返回3小时内原料信息
**************/
ALTER FUNCTION [dbo].[FN_GetMaterialInfo](
@MachineNo varchar(20)
,@DispatchNO varchar(50)
,@DispatchPrior float
,@InspectDate datetime
,@Type int --类型(0:表示返回字符(原料编号1,原料描述1/原料编号2,原料描述2/.....) 1:表示返回字符(原料编号1/原料描述2/.....))
)
returns varchar(8000)
AS
begin
declare @tbMaterialInfo table(seq int identity(1,1),MachineNo varchar(20),MaterialInfo varchar(200))
declare @ReturnMaterialInfo varchar(8000),@TotalTimes int,@m int
declare @i int,@count int
select @ReturnMaterialInfo='',@m=3
select @TotalTimes=@m*3600

--循环查找一天内的加料信息
while (not exists(select * from MES_Material m
where DATEDIFF(ss,m.FeedingTime,@InspectDate)>0
and DATEDIFF(ss,m.FeedingTime,@InspectDate)<@TotalTimes
and MachineNo=@MachineNo and DispatchNO=@DispatchNO
and DispatchPrior=@DispatchPrior
) and @m<=24
)
begin
set @m=@m+3
select @TotalTimes=@m*3600
end


if(@m<=24) /*一天(24小时内)有加料信息*/
begin
if(@Type=0)
begin
insert into @tbMaterialInfo(MachineNo,MaterialInfo)
select distinct MachineNO,m.MaterialNO+'/'+p.FeedGrade+'/'+BatchNO
from MES_Material m
left join MES_MaterialPacket p on m.Supplier=p.SupNO and m.MaterialNO=p.MaterialNO
where DATEDIFF(ss,m.FeedingTime,@InspectDate)>0
and DATEDIFF(ss,m.FeedingTime,@InspectDate)<@TotalTimes
and MachineNo=@MachineNo and DispatchNO=@DispatchNO and DispatchPrior=@DispatchPrior
end
else
begin
insert into @tbMaterialInfo(MachineNo,MaterialInfo)
select distinct MachineNO,p.FeedGrade+'/'+BatchNO
from MES_Material m
left join MES_MaterialPacket p on m.Supplier=p.SupNO and m.MaterialNO=p.MaterialNO
where DATEDIFF(ss,m.FeedingTime,@InspectDate)>0
and DATEDIFF(ss,m.FeedingTime,@InspectDate)<@TotalTimes
and MachineNo=@MachineNo and DispatchNO=@DispatchNO and DispatchPrior=@DispatchPrior
end
end
else /*一天(24小时内)没有该工单的加料信息 取该派工单的第一条加料信息*/
begin
if(@Type=0)
begin
insert into @tbMaterialInfo(MachineNo,MaterialInfo)
select distinct top 1 MachineNO,m.MaterialNO+'/'+p.FeedGrade+'/'+BatchNO
from MES_Material m
left join MES_MaterialPacket p on m.Supplier=p.SupNO and m.MaterialNO=p.MaterialNO
where MachineNo=@MachineNo and DispatchNO=@DispatchNO and DispatchPrior=@DispatchPrior
end
else
begin
insert into @tbMaterialInfo(MachineNo,MaterialInfo)
select distinct top 1 MachineNO,p.FeedGrade+'/'+BatchNO
from MES_Material m
left join MES_MaterialPacket p on m.Supplier=p.SupNO and m.MaterialNO=p.MaterialNO
where MachineNo=@MachineNo and DispatchNO=@DispatchNO and DispatchPrior=@DispatchPrior
end
end
select @count=Count(*),@i=1 from @tbMaterialInfo
while(@i<=@count)
begin
if(@ReturnMaterialInfo='')
select @ReturnMaterialInfo=MaterialInfo from @tbMaterialInfo where seq=@i
else
set @ReturnMaterialInfo=@ReturnMaterialInfo+','+(select MaterialInfo from @tbMaterialInfo where seq=@i)
set @i=@i+1
end
return @ReturnMaterialInfo
end

原文地址:https://www.cnblogs.com/chengjun/p/4798737.html