wodeqq584958184 2018-05-28 01:35 采纳率: 77.8%
浏览 611
已结题

SQL server如何实现以星期制和时间制的组合为插入条件,以实现将数据插入不同的字段中

如题,现在我的存储过程,数据插入的先前条件是,按照每天的时间,
每天早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,本人实习生,刚出来,懂得东西不多,很多代码也是求助网上解决的,望各路大佬、大神相助与小弟~!!感激不尽~!!

  • 写回答

6条回答 默认 最新

  • dabocaiqq 2018-05-28 01:46
    关注

    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 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝