更新-声明一个值并设置不同的结果

时间:2018-08-10 11:57:54

标签: sql sql-server

您好,我不确定这是否可以解决此问题,但如果您有解决方案或替代方法可以解决此问题,将不胜感激。

为了说明,我有一个跟踪号列表,我想将“ ShipDate”声明为“ AddedDate”,然后将“ DueDeliveryDate”声明为“ ShipDate” +天,具体取决于发货的日期。

这里的问题是,它将使用在“ ShipDate”和“ DueDeliveryDate”中都首先声明的相同日期更新所有跟踪号。

请参阅SQL代码:

import re
import dns.resolver
import socket
import smtplib
def email_verifier(email):

    match = re.match(r'([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$', email)
    if(match == None):
        return "Invalid Email."
    else:
        splitEmail = email.split('@')
        domain = str(splitEmail[1])


        records = dns.resolver.query(domain, 'MX')
        mxRecord = records[0].exchange
        mxRecord = str(mxRecord)
        # Get local server hostname
        host = socket.gethostname()

        # SMTP lib setup (use debug level for full output)
        server = smtplib.SMTP()
        server.set_debuglevel(0)

        # SMTP Conversation
        server.connect(mxRecord)
        server.helo(host)
        server.mail('sender@gmail.com')
        code, message = server.rcpt(str(email))
        server.quit()

       #returns 250 if valid
        if code == 250:
            return ('Valid Email Address')
        else:
            return ('invalid Email Address')

email = input("Enter email address : ")
print(email_verifier(email))

这是表格的摘录:

begin tran
Declare @ShipDate varchar(max) = (Select cast(AddedDate as date) from
                                    (select distinct a.TrackingNumber, b.AddedDate 
                                     from ConTransaction a
                                     inner join Consignment b 
                                     on a.TrackingNumber = b.TrackingNumber
                                     where a.TrackingNumber in 
                                                          (
                                                          Select TrackingNumber                                                                    
                                                          from Consignment  C WITH (NOLOCK) 
                                                          Where ShipDate > DateAdd(d, 7, Convert(smalldatetime,Convert(Varchar(10),getdate(),126)))
                                                          AND [Status] = 0                                                     
                                                          ))part01)

Declare @DueDeliveryDate datetime 

Begin

if datename(weekday,@ShipDate)  = 'Friday' 
   set @DueDeliveryDate = dateadd(day,3,@ShipDate)
if datename(weekday,@ShipDate)  = 'Saturday' 
   set @DueDeliveryDate = dateadd(day,2,@ShipDate)
else
   set @DueDeliveryDate = dateadd(day,1,@ShipDate)


Update CN
Set ShipDate = @ShipDate
,DueDeliveryDate = @DueDeliveryDate
,UpdatedBy = 'IT ADMIN'
-- Select *    
FROM dbo.Consignment CN
Where cn.TrackingNumber in (
          select distinct a.TrackingNumber 
          from ConTransaction a
          inner join Consignment b 
          on a.TrackingNumber = b.TrackingNumber
          where a.TrackingNumber in (
                       Select TrackingNumber      
                       from Consignment  C WITH (NOLOCK) 
                       Where ShipDate > DateAdd(d, 7,Convert(smalldatetime,Convert(Varchar(10),getdate(),126))) 
                       and [Status] = 0                                                     
                       and TrackingNumber in ( '65560460605' , '50454646064')
                                            ))
end


--rollback tran

3 个答案:

答案 0 :(得分:2)

Begin

Update CN
Set ShipDate = @ShipDate
,DueDeliveryDate = case 
   when datename(weekday,@ShipDate)  = 'Friday' 
         then dateadd(day,3,@ShipDate)
   when datename(weekday,@ShipDate)  = 'Saturday' 
        then dateadd(day,2,@ShipDate)
   else dateadd(day,1,@ShipDate) end
,UpdatedBy = 'IT ADMIN'
,UpdatedDate = getdate()
-- Select *    
FROM dbo.Consignment CN
Where cn.TrackingNumber in ( **List of tracking number** ) 

答案 1 :(得分:0)

我会用case表达式重写它:

set @DueDeliveryDate = dateadd(day, (case datename(weekday,@ShipDate)
                                          when 'Friday' then 3
                                          when 'Saturday' then 2
                                          else 1 
                                     end), @ShipDate
                              )

update cm
     set ShipDate = @ShipDate,
         DueDeliveryDate = @DueDeliveryDate,
         UpdatedBy = 'IT ADMIN',
         UpdatedDate = getdate()  
from dbo.Consignment cn
where cn.TrackingNumber in ( **List of tracking number** ); 

答案 2 :(得分:0)

请参阅下面的解决方案:

UPDATE a
SET ShipDate = a.AddedDate             
,DueDeliveryDate = CASE
                  WHEN datename(weekday,a.AddedDate)  = 'Friday' THEN 
                  dateadd(day,3,a.AddedDate)
                  WHEN datename(weekday,a.AddedDate)  = 'Saturday' THEN 
                   dateadd(day,3,a.AddedDate) 
                  ELSE dateadd(day,1,a.AddedDate) 
                  END
,UpdatedBy = 'IT ADMIN'
,UpdatedDate = getdate()
-- Select *    
FROM dbo.Consignment a
Where 
TrackingNumber in 
(
SELECT DISTINCT a.TrackingNumber 
FROM ConTransaction a
INNER JOIN Consignment b 
ON a.TrackingNumber = b.TrackingNumber
WHERE a.TrackingNumber in (SELECT TrackingNumber  
                                FROM Consignment  C WITH (NOLOCK) 
                                WHERE ShipDate > DateAdd(d, 7, 
Convert(smalldatetime,Convert(Varchar(10),getdate(),126)))
                                AND [Status] = 0
                                AND TrackingNumber IN
                                         ( 65560460605,
                                           50454646064 )
                            )
)

*所以我用“ AddedDate”更新了“ ShipDate”,然后根据我使用“ AddedDate”更新了“ DueDeliveryDate”的日期进行了修改

如果可以建议其他标题,则可以随时告诉我,这个问题会更好,我会进行更新。

感谢您的帮助!