如题,现在我的存储过程,数据插入的先前条件是,按照每天的时间,
每天早8点到晚8点这个时间段内,数据就往字段"Montime-8:00AM-8:00PM"插入,其余
时间(即晚8点到第二天早上8:00),数据就往另一字段"Montime-8:00PM-8:00AM"插入,
附上我的存储过程代码和table5表格的构造:
table5表格字段构造
目前已实现的时间段不同插入不同字段的存储过程代码:
USE [plc]
GO
/****** Object: StoredProcedure [dbo].[sp_table5_insert] Script Date: 05/28/2018 08:59:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- 插入T5逻辑实现
ALTER proc [dbo].[sp_table5_insert]
@_T4ID BIGINT -- machine-status=0的自增id
as
BEGIN
-- 上一条错误代码记录id
DECLARE @_T4ErrorCodeID BIGINT
-- 分钟时差
DECLARE @_timeDifferenceMin BIGINT
--时间段变量
DECLARE @shi int
set @shi=DateName(hour,GetDate())
-- 开始时间
DECLARE @_machineItime VARCHAR(50)
-- 获取上一条错误代码记录id并且计算以分钟为单位的时差
SELECT TOP 1 @_T4ErrorCodeID = id, @_timeDifferenceMin = DATEDIFF(S,ng_itime,(SELECT machine_itime FROM table4
WHERE id = @_T4ID))
FROM table4
WHERE id < @_T4ID AND location IS NOT NULL AND pointer IS NOT NULL AND ng_itime IS NOT NULL
ORDER BY id DESC;
--判断时间,早8点到晚8点
if(@shi>8 and @shi<20)
begin
-- 对T5表操作,存在类型累加时间,不存在新增
IF EXISTS (SELECT * FROM table5 AS t5 LEFT JOIN table4 AS t4 ON t4.location=t5.location AND t4.pointer=t5.pointer
WHERE t4.id=@_T4ErrorCodeID)
-- 修改
UPDATE table5
SET [Montime_8:00AM-8:00PM] = ISNULL([Montime_8:00AM-8:00PM],0) + @_timeDifferenceMin
FROM table5 AS t5, table4 AS t4
WHERE t4.location=t5.location AND t4.pointer=t5.pointer AND t4.id=@_T4ErrorCodeID;
ELSE
-- 插入
INSERT INTO table5(location,pointer,[Montime_8:00AM-8:00PM])
SELECT location,pointer,@_timeDifferenceMin
FROM table4
WHERE id = @_T4ErrorCodeID;
end
--判断时间,晚8点到早8点
else
begin
-- 对T5表操作,存在类型累加时间,不存在新增
IF EXISTS (SELECT * FROM table5 AS t5 LEFT JOIN table4 AS t4 ON t4.location=t5.location AND t4.pointer=t5.pointer
WHERE t4.id=@_T4ErrorCodeID)
-- 修改
UPDATE table5
SET [Montime_8:00PM-8:00AM] = ISNULL([Montime_8:00PM-8:00AM],0) + @_timeDifferenceMin
FROM table5 AS t5, table4 AS t4
WHERE t4.location=t5.location AND t4.pointer=t5.pointer AND t4.id=@_T4ErrorCodeID;
ELSE
-- 插入
INSERT INTO table5(location,pointer,[Montime_8:00PM-8:00AM])
SELECT location,pointer,@_timeDifferenceMin
FROM table4
WHERE id = @_T4ErrorCodeID;
END
END
现在就是想,SQL server能不能实现以星期制和时间制的组合为条件,来实现插入不同字段
例如:星期一的早8:00到晚8:00,插入这一个字段,晚8:00到第二天星期二的早8:00插入另一个字段,就是要这种效果
或许大家还有什么更好的解决办法可以说出来跟我讨论下,方便讨论我留下我的QQ号码:584958184,本人实习生,刚出来,懂得东西不多,很多代码也是求助网上解决的,望各路大佬、大神相助与小弟~!!感激不尽~!!