create function makereportag (@fdate datetime ,@tdate datetime)
returns @reports table
(machnum varchar(10), --機號
oneday datetime , --日期
hnum varchar(10), --貨號
modnum varchar(10) , --模號
yl varchar(10), --用料
sl int, --數量
yt varchar(10),
bs int ,
pvc int , hip int ,gp int , abss int , pp int , hdpe int , ldpe int , nylon int , kl int , xj int , acetal int , hytrel int , san int , eva int ,pc int
) --用途
as
begin
declare @reportss table
( machnums varchar(10) , onedays datetime , hnums varchar(10), modnums varchar(10) ,
yls varchar(10), sls int, yts varchar(10) ,bs int ,pvc int , hip int ,gp int , abss int ,pp int ,
hdpe int , ldpe int , nylon int , kl int , xj int , acetal int , hytrel int , san int , eva int ,pc int )
declare @machnumt varchar(10)
declare @onedayt datetime
declare @numnamet varchar(10)
declare @modenumt varchar(10)
declare @ylt varchar(10)
declare @kgt int
declare @casett varchar(10)
declare @bs int
declare @bss int
declare @maddate int
declare @causet varchar(10)
declare getcheng cursor for
select a.machnum , a.curday , b.numname , b.modenum ,b.lname , b.kg , a.cause ,a.bs ,a.maddate
from wftablehis b ,machplanhy a
where a.curday=b.curdate
and a.modnum=b.modenum
and a.curday between @fdate and @tdate
open getcheng
fetch next from getcheng into @machnumt ,@onedayt, @numnamet ,@modenumt ,@ylt,@kgt ,@causet ,@bs ,@maddate
while @@fetch_status=0
begin
select @casett=
case when @causet in ('修機','坏機','空機','空閑','停電') then @causet
else '正在運行'
end
select @bss=round((@maddate*3600/@bs),0 )
insert into @reportss (machnums ,onedays ,hnums ,modnums ,yls,sls ,yts ,bs, pvc ,hip ,gp ,abss,pp ,hdpe,ldpe,nylon,kl,xj,acetal,hytrel,san,eva,pc)
values (@machnumt ,@onedayt ,@numnamet ,@modenumt ,@ylt ,@kgt ,@casett ,@bss,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null)
fetch next from getcheng into @machnumt ,@onedayt, @numnamet ,@modenumt ,@ylt,@kgt ,@causet ,@bs ,@maddate
end
close getcheng
deallocate getcheng
insert into @reports select * from @reportss
return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
create procedure totable
(@fdate datetime ,@tdate datetime)
as
begin
insert into tochrp select * from makereportag(@fdate ,@tdate)
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO