MES系统的有用存储过程

USE [ChiefmesNEW]
GO
/****** Object: StoredProcedure [dbo].[st_WMS_ImportStockInBill] Script Date: 10/13/2015 17:30:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--exec st_WMS_ImportStockInBill 'in','superadmin'
ALTER proc [dbo].[st_WMS_ImportStockInBill]
(
@in_InOut varchar(5) = 'In',
@in_User varchar(50) = ''
--@in_Msg varchar(2000) = ''
)
as
delete from WMS_StockIn where Flag=1
if(@in_InOut = 'Out')
begin
--truncate table MES_ImportDispatchorder
--select * from
--insert into MES_StockIn(MO,DispatchQty,MachineNO,MouldNO,ItemNO,SocketNum,StaCode,StandCycle,ActualStartDate,ActualEndDate,StartDate,EndDate)
--select MO,DispatchQty,MachineNO,MouldNO,ItemNO,SocketNum,StaCode,StandCycle,ActualStartDate,ActualEndDate,StartDate,EndDate from MES_DispatchOrder
--update MES_StockIn set Flag=1
select 1
end
else if(@in_InOut='In')
begin
declare @AffactRows int , @DelCount int


--update WMS_StockIn set ItemNO = convert(varchar(50),convert(decimal(38,0),convert(float,ItemNO)))
--where CHARINDEX('E+',ItemNO)>0
--or CHARINDEX('e+',ItemNO)>0

select Seq = IDENTITY(int), Date,StockNO,CurrDate,MachineNo,MO,MESBC,EmpBC,Remark,ItemNo,ItemName, InQty,PlusQty,BillType
, SupplyNO, AutoRptSAP,RecordMonth, UserCode as StockMan
into #t_Stock
from WMS_StockIn s join (select distinct UserCode,Name from sys_user) u on s.StockMan=u.Name

select @AffactRows =@@ROWCOUNT

insert into ChiefWMS.dbo.WMS_StockInPortLog(ActionName,ActionRemark,AffactRows,CreateTime,CreateMan)
select '入库','入库导入行数',@AffactRows, GETDATE(), @in_User

select @DelCount = 0
select @DelCount=COUNT(*) from #t_Stock
where isnull(ltrim(rtrim(ItemNO)),'') = '' or CHARINDEX('E+',ItemNO)>0
or CHARINDEX('e+',ItemNO)>0


delete WMS_StockInHis where ImportTime<DATEADD(DAY, -10,GETDATE())
insert into WMS_StockInHis([Date],StockNO,CurrDate, MO, MESBC, EmpBC, Remark, ItemNO
, itemName, InQty, PlusQty, Flag, BillType,SupplyNO, ImportTime, ImportUser,AutoRptSAP)
select [Date],StockNO, Convert(varchar(10),CurrDate,120),MO, MESBC, EmpBC, Remark, ItemNO
, itemName, InQty, PlusQty, Flag, BillType, SupplyNO,GETDATE(), 'superadmin',AutoRptSAP
from WMS_StockIn
truncate table WMS_StockIn

if(@DelCount>0)
begin
declare @IdList varchar(8000)
select @IdList = ''
select @IdList = case when @IdList = '' then CONVERT(varchar(1000),seq) else @IdList+','+CONVERT(varchar(1000),seq) end
from #t_Stock where isnull(ltrim(rtrim(ItemNO)),'') = '' or CHARINDEX('E+',ItemNO)>0
select Flag = 'F', Msg = '导入'+CONVERT(varchar(1000),@AffactRows)+'条,其中第'+@IdList+'物料编码出现异常,请把单元格转为文本'

insert into ChangHongWMS_904.dbo.WMS_StockInPortLog(ActionName,ActionRemark,AffactRows,CreateTime,CreateMan)
select '入库','入库删除编号为空',@DelCount, GETDATE(), @in_User

end
exec ChiefWMS.dbo.st_WMS_BatchImportStockIn @in_User

end----------------------------

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

USE [ChangHong_612]
GO
/****** Object: UserDefinedFunction [dbo].[FN_GetMaterialInfo] Script Date: 10/12/2015 13:46:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*************
creator: zhuss
create date: 2014.09.25
Module: Public
Remark: 返回3小时内原料信息

B03-850-5# 10024320250001 1.01 2015-10-12 09:14:14.000 0
**************/
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

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

USE [ChiefmesNEW]
GO
/****** Object: StoredProcedure [dbo].[st_MES_RptInspectFirst] Script Date: 10/12/2015 15:32:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author: lxf
-- Create date: 2014.6.30
-- Description: 首检报表
-- st_MES_RptInspectFirst '2015-09-22 08:00:00','2015-10-12 14:15:10','','',''
-- 修改FN_GetSwingCardEmpNameByDis函数调用,添加时间传参 2014.10.28 hz
-- =============================================
ALTER PROCEDURE [dbo].[st_MES_RptInspectFirst]
@StartDate VARCHAR(19),
@EndDate VARCHAR(19),
@ItemNo varchar(50),
@MachineNo varchar(50),
@CheckMan varchar(20)
AS
BEGIN
declare @strSQL nvarchar(4000)
declare @allRecordSQL nvarchar(4000)
declare @strWhere nvarchar(4000)

--建临时表保存品质检验记录
IF object_id('tempdb..#InspectFirst') is not NULL drop table #InspectFirst
CREATE TABLE #InspectFirst
(
ID int ,
BillNO varchar(20),
MO varchar(20) ,
DispatchNo varchar(50),
ActualStartDate datetime,
ActualEndDate datetime,
DispatchPrior float,
ProcCode varchar(20),
StaCode varchar(10),
ItemNO varchar(50) ,
MachineNo varchar(20) ,
WorkMan varchar(2000) ,
CheckMan varchar(20) ,
CheckTime datetime ,
CheckReason varchar(200),
CheckType smallint ,
Remark varchar(200) ,
AbnormalNO varchar(50) ,
WeightSample int ,
OutSample int ,
InkSample int ,
SizeSample int ,
DeformSample int ,
ConfirmMan varchar(20) ,
ConfirmTime datetime ,
CreateMan nvarchar(200) ,
CreateTime datetime ,
UpdateMan nvarchar(200) ,
UpdateTime datetime,
CheckResult varchar(20)
)
set @strWhere=''
set @strSQL='select i.ID ,BillNO ,i.MO ,i.DispatchNo ,d.actualStartDate,d.actualEndDate,i.DispatchPrior ,i.ProcCode ,i.StaCode ,i.ItemNO ,i.MachineNo ,
WorkMan ,i.CheckMan ,CheckTime ,CheckReason ,CheckType ,i.Remark ,AbnormalNO ,WeightSample ,
OutSample ,InkSample ,SizeSample ,DeformSample ,ConfirmMan ,ConfirmTime ,i.CreateMan ,
i.CreateTime ,i.UpdateMan ,i.UpdateTime,CheckResult from MES_Inspect i join mes_dispatchorder d on d.dispatchno=i.dispatchno and d.dispatchprior=i.dispatchprior where checktype=2 '
set @strWhere=@strWhere+' and i.createTime between '''+@StartDate+''' and '''+@EndDate+''''
if @ItemNo<>''
begin
set @strWhere=@strWhere+' and i.itemno like ''%'+@ItemNo+'%'''
end
if @MachineNo<>''
begin
set @strWhere=@strWhere+' and i.machineno like ''%'+@MachineNo+'%'''
end
if @CheckMan<>''
begin
set @strWhere=@strWhere+' and i.createMan like ''%'+@CheckMan+'%'''
end

print @strSQL+@strWhere
insert into #InspectFirst
execute(@strSQL+@strWhere)

update #InspectFirst set WeightSample=(select MAX(WeightSample)from #InspectFirst)
update #InspectFirst set OutSample=(select MAX(OutSample)from #InspectFirst)
update #InspectFirst set InkSample=(select MAX(InkSample)from #InspectFirst)
update #InspectFirst set SizeSample=(select MAX(SizeSample)from #InspectFirst)
update #InspectFirst set DeformSample=(select MAX(DeformSample)from #InspectFirst)
--select * from #InspectFirst
declare @sql varchar(500)
select * from (
select MaterialNO=[dbo].[FN_GetMaterialInfo](c.MachineNo,c.DispatchNo,c.DispatchPrior,w.InspectDate,0)
,c.WeightSample,c.OutSample,c.InkSample,c.SizeSample,c.DeformSample,c.MachineNo,dbo.FN_GetSwingCardEmpNameByDis(c.DispatchNo,c.DispatchPrior,convert(varchar(19),c.CreateTime,120),'') WorkMan,c.AbnormalNO, case when u.Name<>'' then u.Name else u.usercode end CheckMan,c.CheckTime,c.CheckReason,case when u.Name<>'' then u.Name else u.usercode end ConfirmMan,c.Remark,c.CreateTime,i.ItemName,empnamecn CreateMan,w.PartWeight,
case when s.Appearance='' then '-' else s.Appearance end Appearance,case when s.Assembly='' then '-' else s.Assembly end Assembly,case when s.Structure='' then '-' else s.Structure end Structure,
case when s.PerformanceTest3='' then '-' else s.PerformanceTest3 end PerformanceTest3,case when s.PerformanceTest2='' then '-' else s.PerformanceTest2 end PerformanceTest2,case when s.PerformanceTest4='' then '-' else s.PerformanceTest4 end PerformanceTest4,
case when s.PerformanceTest8='' then '-' else s.PerformanceTest8 end PerformanceTest8,case when s.Packing='' then '-' else s.Packing end Packing
,g.LongDownDeformationValue1,g.LongDownDeformationValue2,g.LongDownDeformationValue3,g.LongDownDeformationValue4,g.LongDownDeformationValue5
,g.LongDownGaugeValue1,g.LongDownGaugeValue2,g.LongDownGaugeValue3,g.LongDownGaugeValue4,g.LongDownGaugeValue5
,g.LongMiddleDeformationValue1,g.LongMiddleDeformationValue2,g.LongMiddleDeformationValue3,g.LongMiddleDeformationValue4,g.LongMiddleDeformationValue5
,g.LongMiddleGaugeValue1,g.LongMiddleGaugeValue2,g.LongMiddleGaugeValue3,g.LongMiddleGaugeValue4,g.LongMiddleGaugeValue5
,g.LongUpDeformationValue1,g.LongUpDeformationValue2,g.LongUpDeformationValue3,g.LongUpDeformationValue4,g.LongUpDeformationValue5
,g.LongUpGaugeValue1,g.LongUpGaugeValue2,g.LongUpGaugeValue3,g.LongUpGaugeValue4,g.LongUpGaugeValue5
,g.WidthDownDeformationValue1,g.WidthDownDeformationValue2,g.WidthDownDeformationValue3,g.WidthDownDeformationValue4,g.WidthDownDeformationValue5
,g.WidthDownGaugeValue1,g.WidthDownGaugeValue2,g.WidthDownGaugeValue3,g.WidthDownGaugeValue4,g.WidthDownGaugeValue5
,g.WidthMiddleDeformationValue1,g.WidthMiddleDeformationValue2,g.WidthMiddleDeformationValue3,g.WidthMiddleDeformationValue4,g.WidthMiddleDeformationValue5
,g.WidthMiddleGaugeValue1,g.WidthMiddleGaugeValue2,g.WidthMiddleGaugeValue3,g.WidthMiddleGaugeValue4,g.WidthMiddleGaugeValue5
,g.WidthUpDeformationValue1,g.WidthUpDeformationValue2,g.WidthUpDeformationValue3,g.WidthUpDeformationValue4,g.WidthUpDeformationValue5
,g.WidthUpGaugeValue1,g.WidthUpGaugeValue2,g.WidthUpGaugeValue3,g.WidthUpGaugeValue4,g.WidthUpGaugeValue5
,g.LongUpDeformationResult, g.LongMiddleDeformationResult , g.LongDownDeformationResult , g.WidthUpDeformationResult
, g.WidthMiddleDeformationResult , g.WidthDownDeformationResult
,g.LongUpGaugeResult , g.LongMiddleGaugeResult , g.LongDownGaugeResult , g.WidthUpGaugeResult
, g.WidthMiddleGaugeResult , g.WidthDownGaugeResult
--,case when isnull(g.LongUpDeformationResult,'NG')='OK' and isnull(g.LongMiddleDeformationResult,'NG')='OK' and isnull(g.LongDownDeformationResult,'NG')='OK' and isnull(g.WidthUpDeformationResult,'NG')='OK'
--and isnull(g.WidthMiddleDeformationResult,'NG')='OK' and isnull(g.WidthDownDeformationResult,'NG')='OK' then 'OK' else 'NG' end DeformationResult
--,case when isnull(g.LongUpGaugeResult,'NG')='OK' and isnull(g.LongMiddleGaugeResult,'NG')='OK' and isnull(g.LongDownGaugeResult,'NG')='OK' and isnull(g.WidthUpGaugeResult,'NG')='OK'
--and isnull(g.WidthMiddleGaugeResult,'NG')='OK' and isnull(g.WidthDownGaugeResult,'NG')='OK' then 'OK' else 'NG' end GaugeResult
/*update by zhuss 2014-09-25*/
,DeformationResult=case when ((g.LongUpDeformationResult is null or g.LongUpDeformationResult='') and (g.LongMiddleDeformationResult is null or g.LongMiddleDeformationResult='') and (g.LongDownDeformationResult is null or g.LongDownDeformationResult='')
and (g.WidthUpDeformationResult is null or g.WidthUpDeformationResult='')and (g.WidthMiddleDeformationResult is null or g.WidthMiddleDeformationResult='')
and (g.WidthDownDeformationResult is null or g.WidthDownDeformationResult='')) then '-'
else
case when (isnull(g.LongUpDeformationResult,'OK')='OK' or g.LongUpDeformationResult='') and (isnull(g.LongMiddleDeformationResult,'OK')='OK' or g.LongMiddleDeformationResult='') and (isnull(g.LongDownDeformationResult,'OK')='OK' or g.LongDownDeformationResult='') and
(isnull(g.WidthUpDeformationResult,'OK')='OK' or g.WidthUpDeformationResult='') and (isnull(g.WidthMiddleDeformationResult,'OK')='OK' or g.WidthMiddleDeformationResult='') and (isnull(g.WidthDownDeformationResult,'OK')='OK' or g.WidthDownDeformationResult='') then 'OK' else 'NG' end
end
,GaugeResult= case when((g.LongUpGaugeResult is Null or g.LongUpGaugeResult='') and (g.LongMiddleGaugeResult is Null or g.LongMiddleGaugeResult='') and (g.LongDownGaugeResult is Null or g.LongDownGaugeResult='')
and (g.WidthUpGaugeResult is Null or g.WidthUpGaugeResult='')and (g.WidthMiddleGaugeResult is Null or g.WidthMiddleGaugeResult='')
and (g.WidthDownGaugeResult is Null or g.WidthDownGaugeResult='')) then '-'
else
case when (isnull(g.LongUpGaugeResult,'OK')='OK' or g.LongUpGaugeResult='') and (isnull(g.LongMiddleGaugeResult,'OK')='OK' or g.LongMiddleGaugeResult='') and (isnull(g.LongDownGaugeResult,'OK')='OK' or g.LongDownGaugeResult='')
and (isnull(g.WidthUpGaugeResult,'OK')='OK' or g.WidthUpGaugeResult='') and (isnull(g.WidthMiddleGaugeResult,'OK')='OK' or g.WidthMiddleGaugeResult='') and (isnull(g.WidthDownGaugeResult,'OK')='OK' or g.WidthDownGaugeResult='') then 'OK' else 'NG' end
end
,case when long is not null and longUp is not null then '长:'+cast(long-cast(REPLACE(CAST(longdown as varchar(10)),'-','') as float) as varchar(10)) +'~'+cast(long+longup as varchar(10)) end LongUpDown
,case when Downlong is not null and DownlongUp is not null then '下长:'+cast(Downlong-cast(REPLACE(CAST(Downlongdown as varchar(10)),'-','') as float) as varchar(10)) +'~'+cast(Downlong+Downlongup as varchar(10)) end LongDown
,case when width is not null and widthUp is not null then '宽:'+cast(width-cast(REPLACE(CAST(widthdown as varchar(10)),'-','') as float) as varchar(10))+'~'+cast(width+widthup as varchar(10)) end WidthUpDown
,case when DfLong is not null and DfLongUp is not null then '长:'+cast(DfLong-cast(REPLACE(CAST(DfLongDown as varchar(10)),'-','') as float) as varchar(10))+'~'+cast(DfLong+DfLongup as varchar(10)) end LongDeformationUpDown
,case when Dfwidth is not null and DfWidthUp is not null then '宽:'+cast(Dfwidth-cast(REPLACE(CAST(Dfwidthdown as varchar(10)),'-','') as float) as varchar(10))+'~'+cast(Dfwidth+Dfwidthup as varchar(10)) end WidthDeformationUpDown
,CheckResult
---
,c.DispatchNo,c.DispatchPrior,w.InspectDate
----
--10024320250001
from
#InspectFirst c left join
(select billno,
max(CASE ItemCode WHEN '1' THEN ItemValue END) AS 'Appearance',
max(CASE ItemCode WHEN '2' THEN ItemValue END) AS 'Structure',
max(CASE ItemCode WHEN '3' THEN ItemValue END) AS 'Assembly',
max(CASE ItemCode WHEN '4' THEN ItemValue END) AS 'Packing',
max(CASE ItemCode WHEN '6' THEN ItemValue END) AS 'PerformanceTest3',
max(CASE ItemCode WHEN '5' THEN ItemValue END) AS 'PerformanceTest2',
max(CASE ItemCode WHEN '7' THEN ItemValue END) AS 'PerformanceTest4',
max(CASE ItemCode WHEN '8' THEN ItemValue END) AS 'PerformanceTest8'
from MES_InspectSingle group by billno) s on c.BillNO=s.BillNO left join
(select billno,
max(CASE when ItemType=3 and ItemCode='上长' and Position='长' THEN GaugeValue1 END) AS 'LongUpGaugeValue1',
max(CASE when ItemType=3 and ItemCode='上长' and Position='长' THEN GaugeValue2 END) AS 'LongUpGaugeValue2',
max(CASE when ItemType=3 and ItemCode='上长' and Position='长' THEN GaugeValue3 END) AS 'LongUpGaugeValue3',
max(CASE when ItemType=3 and ItemCode='上长' and Position='长' THEN GaugeValue4 END) AS 'LongUpGaugeValue4',
max(CASE when ItemType=3 and ItemCode='上长' and Position='长' THEN GaugeValue5 END) AS 'LongUpGaugeValue5',
max(CASE when ItemType=3 and ItemCode='中长' and Position='长' THEN GaugeValue1 END) AS 'LongMiddleGaugeValue1',
max(CASE when ItemType=3 and ItemCode='中长' and Position='长' THEN GaugeValue2 END) AS 'LongMiddleGaugeValue2',
max(CASE when ItemType=3 and ItemCode='中长' and Position='长' THEN GaugeValue3 END) AS 'LongMiddleGaugeValue3',
max(CASE when ItemType=3 and ItemCode='中长' and Position='长' THEN GaugeValue4 END) AS 'LongMiddleGaugeValue4',
max(CASE when ItemType=3 and ItemCode='中长' and Position='长' THEN GaugeValue5 END) AS 'LongMiddleGaugeValue5',
max(CASE when ItemType=3 and ItemCode='下长' and Position='长' THEN GaugeValue1 END) AS 'LongDownGaugeValue1',
max(CASE when ItemType=3 and ItemCode='下长' and Position='长' THEN GaugeValue2 END) AS 'LongDownGaugeValue2',
max(CASE when ItemType=3 and ItemCode='下长' and Position='长' THEN GaugeValue3 END) AS 'LongDownGaugeValue3',
max(CASE when ItemType=3 and ItemCode='下长' and Position='长' THEN GaugeValue4 END) AS 'LongDownGaugeValue4',
max(CASE when ItemType=3 and ItemCode='下长' and Position='长' THEN GaugeValue5 END) AS 'LongDownGaugeValue5',
max(CASE when ItemType=3 and ItemCode='左宽' and Position='宽' THEN GaugeValue1 END) AS 'WidthUpGaugeValue1',
max(CASE when ItemType=3 and ItemCode='左宽' and Position='宽' THEN GaugeValue2 END) AS 'WidthUpGaugeValue2',
max(CASE when ItemType=3 and ItemCode='左宽' and Position='宽' THEN GaugeValue3 END) AS 'WidthUpGaugeValue3',
max(CASE when ItemType=3 and ItemCode='左宽' and Position='宽' THEN GaugeValue4 END) AS 'WidthUpGaugeValue4',
max(CASE when ItemType=3 and ItemCode='左宽' and Position='宽' THEN GaugeValue5 END) AS 'WidthUpGaugeValue5',
max(CASE when ItemType=3 and ItemCode='中宽' and Position='宽' THEN GaugeValue1 END) AS 'WidthMiddleGaugeValue1',
max(CASE when ItemType=3 and ItemCode='中宽' and Position='宽' THEN GaugeValue2 END) AS 'WidthMiddleGaugeValue2',
max(CASE when ItemType=3 and ItemCode='中宽' and Position='宽' THEN GaugeValue3 END) AS 'WidthMiddleGaugeValue3',
max(CASE when ItemType=3 and ItemCode='中宽' and Position='宽' THEN GaugeValue4 END) AS 'WidthMiddleGaugeValue4',
max(CASE when ItemType=3 and ItemCode='中宽' and Position='宽' THEN GaugeValue5 END) AS 'WidthMiddleGaugeValue5',
max(CASE when ItemType=3 and ItemCode='右宽' and Position='宽' THEN GaugeValue1 END) AS 'WidthDownGaugeValue1',
max(CASE when ItemType=3 and ItemCode='右宽' and Position='宽' THEN GaugeValue2 END) AS 'WidthDownGaugeValue2',
max(CASE when ItemType=3 and ItemCode='右宽' and Position='宽' THEN GaugeValue3 END) AS 'WidthDownGaugeValue3',
max(CASE when ItemType=3 and ItemCode='右宽' and Position='宽' THEN GaugeValue4 END) AS 'WidthDownGaugeValue4',
max(CASE when ItemType=3 and ItemCode='右宽' and Position='宽' THEN GaugeValue5 END) AS 'WidthDownGaugeValue5',
max(CASE when ItemType=5 and ItemCode='上长' and Position='长' THEN GaugeValue1 END) AS 'LongUpDeformationValue1',
max(CASE when ItemType=5 and ItemCode='上长' and Position='长' THEN GaugeValue2 END) AS 'LongUpDeformationValue2',
max(CASE when ItemType=5 and ItemCode='上长' and Position='长' THEN GaugeValue3 END) AS 'LongUpDeformationValue3',
max(CASE when ItemType=5 and ItemCode='上长' and Position='长' THEN GaugeValue4 END) AS 'LongUpDeformationValue4',
max(CASE when ItemType=5 and ItemCode='上长' and Position='长' THEN GaugeValue5 END) AS 'LongUpDeformationValue5',
max(CASE when ItemType=5 and ItemCode='中长' and Position='长' THEN GaugeValue1 END) AS 'LongMiddleDeformationValue1',
max(CASE when ItemType=5 and ItemCode='中长' and Position='长' THEN GaugeValue2 END) AS 'LongMiddleDeformationValue2',
max(CASE when ItemType=5 and ItemCode='中长' and Position='长' THEN GaugeValue3 END) AS 'LongMiddleDeformationValue3',
max(CASE when ItemType=5 and ItemCode='中长' and Position='长' THEN GaugeValue4 END) AS 'LongMiddleDeformationValue4',
max(CASE when ItemType=5 and ItemCode='中长' and Position='长' THEN GaugeValue5 END) AS 'LongMiddleDeformationValue5',
max(CASE when ItemType=5 and ItemCode='下长' and Position='长' THEN GaugeValue1 END) AS 'LongDownDeformationValue1',
max(CASE when ItemType=5 and ItemCode='下长' and Position='长' THEN GaugeValue2 END) AS 'LongDownDeformationValue2',
max(CASE when ItemType=5 and ItemCode='下长' and Position='长' THEN GaugeValue3 END) AS 'LongDownDeformationValue3',
max(CASE when ItemType=5 and ItemCode='下长' and Position='长' THEN GaugeValue4 END) AS 'LongDownDeformationValue4',
max(CASE when ItemType=5 and ItemCode='下长' and Position='长' THEN GaugeValue5 END) AS 'LongDownDeformationValue5',
max(CASE when ItemType=5 and ItemCode='左宽' and Position='宽' THEN GaugeValue1 END) AS 'WidthUpDeformationValue1',
max(CASE when ItemType=5 and ItemCode='左宽' and Position='宽' THEN GaugeValue2 END) AS 'WidthUpDeformationValue2',
max(CASE when ItemType=5 and ItemCode='左宽' and Position='宽' THEN GaugeValue3 END) AS 'WidthUpDeformationValue3',
max(CASE when ItemType=5 and ItemCode='左宽' and Position='宽' THEN GaugeValue4 END) AS 'WidthUpDeformationValue4',
max(CASE when ItemType=5 and ItemCode='左宽' and Position='宽' THEN GaugeValue5 END) AS 'WidthUpDeformationValue5',
max(CASE when ItemType=5 and ItemCode='中宽' and Position='宽' THEN GaugeValue1 END) AS 'WidthMiddleDeformationValue1',
max(CASE when ItemType=5 and ItemCode='中宽' and Position='宽' THEN GaugeValue2 END) AS 'WidthMiddleDeformationValue2',
max(CASE when ItemType=5 and ItemCode='中宽' and Position='宽' THEN GaugeValue3 END) AS 'WidthMiddleDeformationValue3',
max(CASE when ItemType=5 and ItemCode='中宽' and Position='宽' THEN GaugeValue4 END) AS 'WidthMiddleDeformationValue4',
max(CASE when ItemType=5 and ItemCode='中宽' and Position='宽' THEN GaugeValue5 END) AS 'WidthMiddleDeformationValue5',
max(CASE when ItemType=5 and ItemCode='右宽' and Position='宽' THEN GaugeValue1 END) AS 'WidthDownDeformationValue1',
max(CASE when ItemType=5 and ItemCode='右宽' and Position='宽' THEN GaugeValue2 END) AS 'WidthDownDeformationValue2',
max(CASE when ItemType=5 and ItemCode='右宽' and Position='宽' THEN GaugeValue3 END) AS 'WidthDownDeformationValue3',
max(CASE when ItemType=5 and ItemCode='右宽' and Position='宽' THEN GaugeValue4 END) AS 'WidthDownDeformationValue4',
max(CASE when ItemType=5 and ItemCode='右宽' and Position='宽' THEN GaugeValue5 END) AS 'WidthDownDeformationValue5',
--max(CASE when ItemType=3 and ItemCode='上长' and Position='长' THEN isnull(CheckResult,'NG') END) AS 'LongUpGaugeResult',
--max(CASE when ItemType=3 and ItemCode='中长' and Position='长' THEN isnull(CheckResult,'NG') END) AS 'LongMiddleGaugeResult',
--max(CASE when ItemType=3 and ItemCode='下长' and Position='长' THEN isnull(CheckResult,'NG') END) AS 'LongDownGaugeResult',
--max(CASE when ItemType=3 and ItemCode='左宽' and Position='宽' THEN isnull(CheckResult,'NG') END) AS 'WidthUpGaugeResult',
--max(CASE when ItemType=3 and ItemCode='中宽' and Position='宽' THEN isnull(CheckResult,'NG') END) AS 'WidthMiddleGaugeResult',
--max(CASE when ItemType=3 and ItemCode='右宽' and Position='宽' THEN isnull(CheckResult,'NG') END) AS 'WidthDownGaugeResult',
--max(CASE when ItemType=5 and ItemCode='上长' and Position='长' THEN isnull(CheckResult,'NG') END) AS 'LongUpDeformationResult',
--max(CASE when ItemType=5 and ItemCode='中长' and Position='长' THEN isnull(CheckResult,'NG') END) AS 'LongMiddleDeformationResult',
--max(CASE when ItemType=5 and ItemCode='下长' and Position='长' THEN isnull(CheckResult,'NG') END) AS 'LongDownDeformationResult',
--max(CASE when ItemType=5 and ItemCode='左宽' and Position='宽' THEN isnull(CheckResult,'NG') END) AS 'WidthUpDeformationResult',
--max(CASE when ItemType=5 and ItemCode='中宽' and Position='宽' THEN isnull(CheckResult,'NG') END) AS 'WidthMiddleDeformationResult',
--max(CASE when ItemType=5 and ItemCode='右宽' and Position='宽' THEN isnull(CheckResult,'NG') END) AS 'WidthDownDeformationResult'
/*Update by zhuss 2014-09-25*/
max(CASE when ItemType=3 and ItemCode='上长' and Position='长' THEN CheckResult END) AS 'LongUpGaugeResult',
max(CASE when ItemType=3 and ItemCode='中长' and Position='长' THEN CheckResult END) AS 'LongMiddleGaugeResult',
max(CASE when ItemType=3 and ItemCode='下长' and Position='长' THEN CheckResult END) AS 'LongDownGaugeResult',
max(CASE when ItemType=3 and ItemCode='左宽' and Position='宽' THEN CheckResult END) AS 'WidthUpGaugeResult',
max(CASE when ItemType=3 and ItemCode='中宽' and Position='宽' THEN CheckResult END) AS 'WidthMiddleGaugeResult',
max(CASE when ItemType=3 and ItemCode='右宽' and Position='宽' THEN CheckResult END) AS 'WidthDownGaugeResult',
max(CASE when ItemType=5 and ItemCode='上长' and Position='长' THEN CheckResult END) AS 'LongUpDeformationResult',
max(CASE when ItemType=5 and ItemCode='中长' and Position='长' THEN CheckResult END) AS 'LongMiddleDeformationResult',
max(CASE when ItemType=5 and ItemCode='下长' and Position='长' THEN CheckResult END) AS 'LongDownDeformationResult',
max(CASE when ItemType=5 and ItemCode='左宽' and Position='宽' THEN CheckResult END) AS 'WidthUpDeformationResult',
max(CASE when ItemType=5 and ItemCode='中宽' and Position='宽' THEN CheckResult END) AS 'WidthMiddleDeformationResult',
max(CASE when ItemType=5 and ItemCode='右宽' and Position='宽' THEN CheckResult END) AS 'WidthDownDeformationResult'
from MES_InspectGauge group by billno) g on g.BillNO=s.BillNO
--left join
--(
-- select distinct a.DispatchNO,a.MachineNO,a.MaterialNO,MAX(a.ID) as ID
-- from MES_Material a
-- join #InspectFirst n on a.DispatchNO=n.DispatchNo and n.MachineNo=a.MachineNO
-- where a.FeedingTime>DATEADD(HH,-2,GETDATE())
-- group by a.DispatchNO,a.MachineNO,a.MaterialNO
-- ) m on m.DispatchNO=c.DispatchNo and m.MachineNO=c.MachineNo
left join MES_InspectWeightRec w on c.BillNO=w.BillNO
left join MES_Item i on c.ItemNO=i.itemno
left join MES_Employee e on e.EmpID=c.CreateMan
left join Sys_User u on u.usercode=c.CheckMan) a order by createtime desc
drop table #InspectFirst
END

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

USE [ChiefmesNEW]
GO
/****** Object: StoredProcedure [dbo].[st_MES_FinalInspection] Script Date: 10/12/2015 10:35:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author: hz
-- Create date: 2014.6.25
-- Description: 查询品质检验数据
-- st_MES_FinalInspection '','','','2015-10-10 08:00','2015-10-12 08:00',200,1,'',''
-- =============================================

ALTER PROCEDURE [dbo].[st_MES_FinalInspection]
@BatchNo varchar(20),
@ItemNo varchar(50),
@CheckResult varchar(30),
@BeginDate varchar(20),
@EndDate varchar(20),
@Pagesize int,
@Pageindex int,
@InspectMan varchar(50)='',
--add by zhuss 2015-04-02
@CheckType varchar(50)='',

@BillNO nvarchar(50)=''
AS
BEGIN
declare @strSQL nvarchar(4000)
declare @strWhere nvarchar(4000)

--建临时表保存品质检验记录
IF object_id('tempdb..#FinalInspection') is not NULL drop table #FinalInspection
CREATE TABLE #FinalInspection
(
--ID int identity(1,1),
BillNO nvarchar(50),
ItemNO nvarchar(50),
ItemName nvarchar(500),
BatchNo nvarchar(50),
CreateMan varchar(200),
CreateTime datetime ,
CheckType varchar(50) ,
ConfirmMan nvarchar(50) ,
ConfirmTime datetime,
CheckMan nvarchar(50),
CheckTime datetime,
ProductionTime nvarchar(50),
CheckResult nvarchar(100),
AbnormalNo nvarchar(100),
NoRemark nvarchar(600),
ProdBCCode nvarchar(50),
SpecificGroups nvarchar(50),
ProdNum int
)
set @strWhere=''
set @strSQL='insert into #FinalInspection select * from(
select a.BillNO ,
a.ItemNO,
b.ItemName,
a.BatchNo,
a.CreateMan,
a.CreateTime ,
a.CheckType,
a.ConfirmMan ,
a.ConfirmTime,
a.CheckMan,
a.CheckTime,
ProductionTime,a.CheckResult,AbnormalNo,a.NoRemark,
case when a.ProdBCCode=''AC'' then ''白班'' else ''晚班'' end ProdBCCode,
SpecificGroups,a.ProdNum from MES_Inspect a left join MES_Item b on a.ItemNO=b.ItemNO '
--set @strWhere=@strWhere+' ) a where 1=1 and CheckType in (5,6)'
--update by zhuss 2015-04-02
declare @strCheckType varchar(100)
select @strCheckType=case when @CheckType='' then ' and CheckType in(5,6)' else ' and CheckType='+@CheckType end
set @strWhere=@strWhere+' ) a where 1=1 '+@strCheckType
--if(@InspectMan<>'')
--begin
-- set @strWhere=@strWhere+' and CreateMan='''+@InspectMan+''''
--end

if @BatchNo<>''
begin
set @strWhere=@strWhere+' and BatchNo like ''%'+@BatchNo+'%'''
end

if @ItemNo<>''
begin
set @strWhere=@strWhere+' and ItemNo like ''%'+@ItemNo+'%'''
end

if @CheckResult<>''
begin
set @strWhere=@strWhere+' and CheckResult like ''%'+@CheckResult+'%'''
end
--20150925ST
if @BillNO<>''
begin
set @strWhere=@strWhere+' and BillNO like ''%'+@BillNO+'%'''
end

--20150925End


if @BeginDate<>''
begin
set @strWhere=@strWhere+' and convert(varchar(19),CreateTime,121) >='''+convert(varchar(10),@BeginDate,121)+''''
end
if @EndDate<>''
begin
set @strWhere=@strWhere+' and convert(varchar(19),CreateTime,121) <='''+convert(varchar(10),@EndDate,121)+''''
end
set @strWhere=@strWhere+' order by Createtime desc'

print @strSQL+@strWhere
execute(@strSQL+@strWhere)
update f set CreateMan=isnull(u.Name,e.EmpNameCN) from #FinalInspection f
left join MES_Employee e on e.EmpID=f.CreateMan
left join Sys_User u on u.UserCode=f.CreateMan

update n set CheckMan=isnull(u.Name,e.EmpNameCN) from #FinalInspection n
left join Sys_User u on n.CheckMan=u.UserCode
left join MES_Employee e on n.CheckMan=e.EmpID

declare @CreateManCn varchar(50)
if(@InspectMan<>'')
begin
select @CreateManCn=Name from Sys_User where UserCode like '%'+@InspectMan+'%' or Name like '%'+@InspectMan+'%'
if(@CreateManCn='' or @CreateManCn is null)
begin
select @CreateManCn=EmpNameCN from MES_Employee where EmpID like '%'+@InspectMan+'%' or EmpNameCN like '%'+@InspectMan+'%'
end
delete a from #FinalInspection a
LEFT join (select * from #FinalInspection where CreateMan=@CreateManCn) b on a.BillNO=b.BillNO
where b.BillNO is null
end

alter table #FinalInspection
add ID int not null identity(1,1)

declare @row_from int, @row_to int,@out_total_rows int
select @out_total_rows = count(*) from #FinalInspection
print @out_total_rows
execute st_MES_RS_Pages @out_total_rows, @Pagesize, @Pageindex, @row_from output, @row_to output

select * from #FinalInspection
where ID between @row_from and @row_to
order by ID
select @out_total_rows
END

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