SQL How to correctly set a date variable value and use it?

后端 未结 2 1409
伪装坚强ぢ
伪装坚强ぢ 2021-01-17 09:45

I have the following query which uses a date variable, which is generated inside the stored procedure:

DECLARE @sp_Date DATETIME
SET @sp_Date = DateAdd(m, -6,         


        
2条回答
  •  时光说笑
    2021-01-17 10:16

    If you manually write out the query with static date values (e.g. '2009-10-29 13:13:07.440') do you get any rows?

    So, you are saying that the following two queries produce correct results:

    SELECT DISTINCT pat.PublicationID
    FROM PubAdvTransData AS pat 
        INNER JOIN PubAdvertiser AS pa 
            ON pat.AdvTransID = pa.AdvTransID
    WHERE (pat.LastAdDate > '2009-10-29 13:13:07.440') AND (pa.AdvertiserID = 12345))
    
    DECLARE @sp_Date DATETIME
    SET @sp_Date = '2009-10-29 13:13:07.440'
    
    SELECT DISTINCT pat.PublicationID
    FROM PubAdvTransData AS pat 
        INNER JOIN PubAdvertiser AS pa 
            ON pat.AdvTransID = pa.AdvTransID
    WHERE (pat.LastAdDate > @sp_Date) AND (pa.AdvertiserID = 12345))
    

提交回复
热议问题