USE [EFNETSYS] GO /****** Object: StoredProcedure [dbo].[SP_Dz] Script Date: 05/26/2018 16:14:10 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[SP_Dz] ( @註銷時間 int = 360 ) AS BEGIN SET NOCOUNT ON; -------------------------------------------------------------- if object_id('tempdb..#TempA') is not null drop table #TempA; -------------------------------------------------------------- DECLARE @Time nchar(20) = replace(Convert(nchar(20),GETDATE(),120),'-','/') DECLARE @Run_Time_ss int , @XUHAO varchar(8) set @Run_Time_ss = Convert(int,substring(@Time,18,2)) + Convert(int,substring(@Time,15,2)) * 60 + Convert(int,substring(@Time,12,2)) * 360 select * into #TempA from ( select ROW_NUMBER() OVER (ORDER BY @XUHAO ASC) AS '序號' ,* from ( select ZZ001 as 登陸者 ,ZY002 as 起始時間 ,@Time as 當前時間 ,@Run_Time_ss - ( Convert(int,substring(ZY002,18,2)) + Convert(int,substring(ZY002,15,2)) * 60 + Convert(int,substring(ZY002,12,2)) * 360) as 運行時間 ,ZZ004 from EFNETSYS.dbo.CRMZZ left join EFNETSYS.dbo.CRMZY on ZZ001 = ZY001 and ZZ004 = ZY004 ) as A where 運行時間 > @註銷時間 ) as B --------------------------------------------------------------- DECLARE @i int = 1 ,@rows int = (select COUNT(*) from #TempA) ,@ZZ004 nchar(20) if @rows <> 0 begin while @i <= @rows begin select @ZZ004 = ZZ004 from #TempA where 序號 = @i delete from EFNETSYS..CRMZZ where ZZ004 = @ZZ004 set @i = @i + 1 end end drop table #TempA --------------------------------------------------------------- End GO