如题,现在我的存储过程,数据插入的先前条件是,按照每天的时间,
每天早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,本人实习生,刚出来,懂得东西不多,很多代码也是求助网上解决的,望各路大佬、大神相助与小弟~!!感激不尽~!!
IF OBJECT_ID ('dbo.varbin2hexstr') IS NOT NULL
DROP FUNCTION dbo.varbin2hexstr
GO
CREATE function varbin2hexstr(
@bin varbinary(8000)
)returns varchar(8000)
as
begin
declare @re varchar(8000),@i int
select @re='',@i=datalength(@bin)
while @i>0
select @re=substring('0123456789ABCDEF',substring(@bin,@i,1)/16+1,1)
+substring('0123456789ABCDEF',substring(@bin,@i,1)%16+1,1)
+@re
,@i=@i-1
-- return('0x'+@re)
return @re
end
GO
IF OBJECT_ID ('dbo.varbin2hexstr') IS NOT NULL
DROP FUNCTION dbo.varbin2hexstr
GO
CREATE function varbin2hexstr(
@bin varbinary(8000)
)returns varchar(8000)
as
begin
declare @re varchar(8000),@i int
select @re='',@i=datalength(@bin)
while @i>0
select @re=substring('0123456789ABCDEF',substring(@bin,@i,1)/16+1,1)
+substring('0123456789ABCDEF',substring(@bin,@i,1)%16+1,1)
+@re
,@i=@i-1
-- return('0x'+@re)
return @re
end
GO
这问题这么难吗???0.0 我原以为只是创建一个变量,赋值给星期函数,然后与时间段条件合并一起就阔以实现了 似乎不能这么简单就实现呢。。。
定时任务不是可以控制吗。。。
还有没有人有其他想法啊。。。我真的是小白一枚。。。这个效果该怎么实现。。。
构造SQL,然后把字段用变量替换不就行了?
大概就是下面的意思,可能有些错误,你看着整一下吧。
if(@shi>8 and @shi<20)
set @colname='Montime_8:00AM-8:00PM'
else
set @colname='Montime_8:00PM-8:00AM'
set @sql='IF EXISTS (SELECT 1 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 ['+@colname+'] = ISNULL(['+@colname+'],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,['+@colname+'])
SELECT location,pointer,'+@_timeDifferenceMin+'
FROM table4
WHERE id = '+@_T4ErrorCodeID+'; '
exec(@sql)
谢谢 问题我自己已经解决了 非常感谢大家~!!