USE [ChiefMes]
GO
/****** Object: StoredProcedure [dbo].[SELECT_TotalNum] Script Date: 05/12/2015 09:08:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/**********************************
Author:Derek
CreateDate:2008-12-18 2008-03-25
EXEC SELECT_TotalNum 'M0001','0001','I0001','2008-03-27 00:00','2008-03-27 23:59'
EXEC SELECT_TotalNum 'IM022408','MO10110211','2100501409','2008-07-19 12:00:00','2009-07-19 23:59:00'
EXEC SELECT_TotalNum '909030276001','IM022413','MO10360211','2122402728','2009-04-03 00:00:00','2009-04-03 23:59:00'
Function:查询啤数序号
**********************************/
ALTER PROCEDURE [dbo].[SELECT_TotalNum]
(
@Dispatchorder varchar(100),
@MachineNo VARCHAR(100),
@MouldNo VARCHAR(100),
@ProductNo VARCHAR(100),
@bDate VARCHAR(19),
@eDate VARCHAR(19)
)
AS
CREATE TABLE #TEMP
(
SelectID INT,
RowID INT,
ID INT,
TotalNum INT
)
declare @ID int,@TotalNum int,@Count int,@I int
set @I=1
select @Count=count(*)
from (
SELECT distinct TotalNum
FROM DataHistory
WHERE 1=1 AND Dispatchorder= @Dispatchorder --and clientip=@MachineNo --AND MachineNo=@MachineNo --AND MouldNo=@MouldNo AND ProductNo=@ProductNo
AND CONVERT(CHAR(19),BeginCycle,121)>=@bDate
AND CONVERT(CHAR(19),BeginCycle,121)<=@eDate
) a
DECLARE _CURSOR cursor for
SELECT DISTINCT ID,TotalNum
FROM DataHistory
WHERE 1=1 AND Dispatchorder= @Dispatchorder --and clientip=@MachineNo --AND MachineNo=@MachineNo --AND MouldNo=@MouldNo AND ProductNo=@ProductNo
AND CONVERT(CHAR(19),BeginCycle,121)>=@bDate
AND CONVERT(CHAR(19),BeginCycle,121)<=@eDate
ORDER BY TotalNum DESC
open _CURSOR
fetch next from _CURSOR into @ID,@TotalNum
while(@@fetch_status=0)
begin
if not exists(select TotalNum from #TEMP where TotalNum=@TotalNum)
begin
insert into #TEMP(SelectID,RowID,ID,TotalNum) values(@I,@Count,@ID,@TotalNum)
set @I=@I+1
set @Count=@Count-1
end
fetch next from _CURSOR into @ID,@TotalNum
end
close _CURSOR
deallocate _CURSOR
select * from #TEMP order by TotalNum DESC
drop table #TEMP
GO