如何在大型机启动时执行查询?

时间:2016-01-14 18:08:18

标签: sql sql-server sql-server-2008 tsql

我有一个查询,只有在大型机启动时才能执行。大型机在这几个小时内停机 星期二到星期六 - 凌晨1点到早上6点, 星期日 - 凌晨1点 - 12点。

我正在尝试声明一个获取当前日期和时间的变量,然后尝试在if语句中编写逻辑以满足上述条件。

if @new_que_stat_cd = 'U'
BEGIN

--declare @recv_time datetime  = getdate()
--if  CASE WHEN datepart(dw,@recv_time) in ('SUNDAY') AND 
--,'TUESDAY','WEDNESDAY','
--declare @ident int =  IDENT_CURRENT( 'SADEV.RO_Transcript.ETQueueCtrl' )
UPDATE NEW SET [ETQueueCtrlStatusCd] = 'U'
       from sadev.RO_Transcript.ETQueueCtrl NEW
       where new.[ETQueueCtrlStatusCd] = 'S'  
       AND new.ErrorFl = 'N'
       and new.VendorTransactionID is not null
       and new.VendorTransactionIDRcvdDate is not null
       and new.ETQueueCtrlID = @queuectrl_id

select new.VendorTransactionIDRcvdDate
from sadev.RO_Transcript.ETQueueCtrl NEW
where new.[ETQueueCtrlStatusCd] = @new_que_stat_cd 
       AND new.ErrorFl = 'N'
       and new.VendorTransactionID is not null

       and new.ETQueueCtrlID = @queuectrl_id
END

此逻辑是否适用于上述条件

 declare @recv_time datetime  = getdate()
if   (convert(varchar,(datepart(DAY,@recv_time))) in ('SUNDAY') 
                    AND convert(varchar, @recv_time, 114) between convert(datetime,'01:00') and convert(datetime,'12:00')) 
                    or
     convert(varchar,(datepart(DAY,@recv_time))) in ('Tuesday','Wednesday','Thursday','Friday','Saturday') 
     AND convert(varchar, @recv_time, 114) between convert(datetime,'01:00') and convert(datetime,'06:00') 

1 个答案:

答案 0 :(得分:-1)

基于:DATEPART (Transact-SQL)

试试这个:

IF @new_que_stat_cd = 'U'
BEGIN
DECLARE @currDate DATETIME=GETDATE();
DECLARE @runProcess BIT=0;
SELECT @runProcess=CASE
                       WHEN DATEPART(dw, @currDate) IN
                                                       (3, 4, 5, 6, 7
                                                       )
                       THEN CASE
                                WHEN DATEPART(hh, @currDate) BETWEEN 1 AND 6
                                THEN 1
                                ELSE 0
                            END
                       WHEN DATEPART(dw, @currDate) IN
                                                       (1
                                                       )
                       THEN CASE
                                WHEN DATEPART(hh, @currDate) BETWEEN 1 AND 12
                                THEN 1
                                ELSE 0
                            END
                       ELSE 0
                   END;
IF @runProcess = 1
BEGIN
UPDATE NEW
       SET ETQueueCtrlStatusCd='U'
FROM sadev.RO_Transcript.ETQueueCtrl NEW
WHERE new.ETQueueCtrlStatusCd = 'S'
      AND new.ErrorFl = 'N'
      AND new.VendorTransactionID IS NOT NULL
      AND new.VendorTransactionIDRcvdDate IS NOT NULL
      AND new.ETQueueCtrlID = @queuectrl_id;
SELECT new.VendorTransactionIDRcvdDate
FROM sadev.RO_Transcript.ETQueueCtrl NEW
WHERE new.ETQueueCtrlStatusCd = @new_que_stat_cd
      AND new.ErrorFl = 'N'
      AND new.VendorTransactionID IS NOT NULL
      AND new.ETQueueCtrlID = @queuectrl_id;
END;
END;

3,4,5,6,7分别为周二至周六,1日为周日。