在上下文中指定的非布尔类型的表达式,其中条件预期在'END'附近

时间:2016-11-16 01:16:47

标签: sql sql-server tsql

所以我正在运行此查询并继续获取上述错误 消息4145,级别15,状态1,过程GetQN8,行1592 在预期条件的上下文中指定的非布尔类型的表达式,靠近'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条记录。

1 个答案:

答案 0 :(得分: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。如果不是,请回复评论