2016-11-16 55 views
0

所以我運行此查詢並繼續獲得上述錯誤 Msg 4145,Level 15,State 1,Procedure GetQN8,1592行 在'END'附近預期條件的上下文中指定的非布爾類型的表達式。。有人能幫助我,我哪裏錯了?在上下文中指定的非布爾類型的表達式,其中條件期望在'END'附近

這是代碼 -

 USE [MailActivity] 
     GO 

     IF OBJECT_ID('GetQN8') IS NULL 
     EXEC('CREATE PROCEDURE GetQN8 AS SET NOCOUNT ON;') 
     GO 

     ALTER PROCEDURE GetQN8 (@CampaignName VARCHAR(200),@EngagedMonths INT, @Active VARCHAR(10), @NotInGroup INT, @CampaignNameForDupCheck VARCHAR(200)) 
     AS 
     BEGIN 
     SET NOCOUNT ON 

    -- CREATE TEMP TABLES DROP TABLE #tempParticipantDataQN8 
    CREATE TABLE #tempParticipantDataQN8 
    (
      ParticipantId INT PRIMARY KEY, 
      GroupId INT, 
      EmailAddress VARCHAR(200), 
      FirstName VARCHAR(100), 
      ActiveYN CHAR(1), 
      NewsLetterStatus CHAR(1), 
      ExpirationDate DATETIME 
    ) 

    -- CREATE INDEX 
    CREATE NONCLUSTERED INDEX [IX_TempParticipantDataEmail] ON #tempParticipantDataQN8 
    (
      [EmailAddress] ASC 
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 

    -- DROP TABLE #tempParticipantAuditDataQN8 
    CREATE TABLE #tempParticipantAuditDataQN8 
    (
      ParticipantId INT, 
      ColumnName VARCHAR(100), 
      AfterChangeValue VARCHAR(100), 
      LastChangedDate DATETIME, 
      LastChangedUser VARCHAR(100) 
    ) 

    -- CREATE INDEX 
    CREATE NONCLUSTERED INDEX [IX_TempParticipantAuditDataColumnName] ON #tempParticipantAuditDataQN8 
    (
      [ColumnName] ASC 
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 

    CREATE NONCLUSTERED INDEX [IX_TempParticipantAuditDataPartId] ON #tempParticipantAuditDataQN8 
    (
      [ParticipantId] ASC 
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 

    INSERT INTO #tempParticipantDataQN8 (ParticipantId,EmailAddress,FirstName,ActiveYN,NewsLetterStatus,GroupId,ExpirationDate) 
    SELECT p.ParticipantId, 
      p.[Preferred Email], 
      p.[First Name], 
      p.ActiveYN, 
      p.NewsLetterStatus, 
      p.GroupId, 
      p.expirationdate 
    FROM ids_reporting.ids_reporting.dbo.Participant p WITH (NOLOCK) 
    JOIN ids_reporting.ids_reporting.dbo.[Group] gr WITH (NOLOCK) 
    ON p.groupId = gr.groupId 
    WHERE 1=1 
    AND ((p.[Preferred Email] NOT LIKE '%@none.com') AND (p.[Preferred Email] IS NOT NULL)) -- Has an email address 
    AND ISNULL(gr.parentGroupId,'') <> 1269 

    -- PULL AUDIT 
    INSERT INTO #tempParticipantAuditDataQN8 (  ParticipantId, ColumnName, AfterChangeValue, LastChangedDate, LastChangedUser) 
    SELECT a.ParticipantId, ColumnName, AfterChangeValue, LastChangedDate, LastChangedUser 
    FROM ids_reporting.ids_reporting.dbo.Participant_Audit a 
    JOIN #tempParticipantDataQN8 p 
    ON p.ParticipantId = a.ParticipantId 
    AND a.ColumnName = 'NewsLetterStatus' 
    AND a.AfterChangeValue = 'U' 

    --Query 
    SELECT DISTINCT(p.[EmailAddress]) AS [EmailAddress], 
      p.FirstName AS [FirstName], 
      p.GroupId AS [GroupId] 
    FROM [CampaignLists],#tempParticipantDataQN8 p 

    LEFT OUTER JOIN MailActivity.dbo.SupressionList08012016 sup 
    ON p.[EmailAddress] = sup.EmailAddress 

    JOIN MailActivity.dbo.MailActivities maEng WITH (NOLOCK) 
    ON (p.[EmailAddress] = maEng.EmailAddress 
     AND maEng.EventType = 'MessageGears.Model.Generated.OpenActivity' 
     AND maEng.EventTimestamp >= DATEADD(month, @EngagedMonths, GETDATE()) 
     ) 

    LEFT OUTER JOIN 
    (
      SELECT DISTINCT(EmailAddress) AS [EmailAddress] 
      FROM MailActivity.dbo.MailActivities WITH (NOLOCK) 
      WHERE EventType = 'MessageGears.Model.Generated.BouncedActivity' 
      AND CategoryCode NOT IN ('10', '30') 
      GROUP BY EmailAddress 
      HAVING COUNT(*) >= 3 
    ) AS SoftBounce 
    ON p.[EmailAddress] = SoftBounce.EmailAddress 
    LEFT OUTER JOIN MailActivity.dbo.MailActivities spam WITH (NOLOCK) 
      ON (p.[EmailAddress] = spam.EmailAddress 
       AND spam.EventType IN ('MessageGears.Model.Generated.UnsubActivity', 'MessageGears.Model.Generated.SpamComplaintActivity')) 
    WHERE 1=1 
    AND SoftBounce.EmailAddress IS NULL -- No soft bounces >= 3 
    AND spam.EmailAddress IS NULL -- No spam complaints 
    AND sup.EmailAddress IS NULL -- No suppression list 
    AND p.ActiveYN = @Active 
    AND [CampaignLists].CampaignName = @CampaignName 


    AND ((p.[EmailAddress] NOT LIKE '%@none.com') AND (p.[EmailAddress] IS NOT NULL)) -- Has an email address 
    AND p.GroupId NOT IN (@NotInGroup) 

    AND NOT EXISTS (SELECT 1 FROM MailActivity.dbo.MailActivities 
         WHERE EmailAddress = p.[EmailAddress] 
         AND RequestId IN 
           (SELECT DISTINCT(RequestId) FROM [CampaignLists] 
           WHERE CampaignName = @CampaignNameForDupCheck 
           ) 
     ) 
    END 

更新:所以我感動[CampaignLists] .CampaignName回WHERE子句,因爲我需要搶在@CampaignName同時還忽略重複記錄記錄在最後由@CampaignNameForDupCheck檢查的另一個廣告系列/表中。執行更新的查詢現在給我0記錄。

+0

愚蠢的問題:在'END'之後可能丟失分號是罪魁禍首嗎? –

+0

END後分號無效。 – ross

回答

1

我相信你缺少EXISTS/NOT EXISTS在最後一次查詢的最後一個條件,在過程

AND EXISTS (SELECT * FROM MailActivity.dbo.MailActivities 
        WHERE EmailAddress = p.[EmailAddress] 
        AND RequestId IN 
          (SELECT DISTINCT(RequestId) FROM [CampaignLists] 
          WHERE 1=1 
          AND CampaignName = @CampaignName 
          AND RequestId <> @CampaignNameForDupCheck 
          ) 
    ) 

望着子查詢中的*和我都認爲這個關聯條件EmailAddress = p.[EmailAddress]你需要EXISTS。如果不是,那麼回覆評論

+0

好抓!所以我更新了代碼以使用NOT EXISTS,因爲我只需要CampaignName中的記錄,而忽略CampaignNameForDupCheck中的重複記錄。你可以讓我知道什麼導致查詢在更新後給出0條記錄嗎?在FROM子句中也包含了表名,但我認爲這是問題所在 – ross

相關問題