2016-11-16 10 views
0

このクエリを実行していて、上記のエラーが発生することがあります。 メッセージ4145、レベル15、状態1、プロシージャGetQN8、Line 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 

アップデート:私は@CampaignNameにあるレコードを取得する必要がありとしても、重複を無視してだから私は、WHERE句に[CampaignLists] .CampaignName背中を動かし @CampaignNameForDupCheckによって最後にチェックされている別のキャンペーン/テーブルのレコード。更新されたクエリを実行すると、現在0レコードになります。

+0

愚かな質問:「END」の後ろにセミコロンがない場合は、ここでの原因になりますか? –

+0

ENDの後にセミコロンを付けると機能しませんでした。 – ross

答えて

1

私はあなたが

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 
          ) 
    ) 

は、私はあなたがEXISTSを必要と仮定したサブクエリと、この相関条件EmailAddress = p.[EmailAddress]内部*を見てみると、あなたの手順で最後のクエリの最後の条件でEXISTS/NOT EXISTSが欠落していると信じています。そうでない場合は、コメントに返信してください。

+0

良いキャッチ!そこで、CampaignNameForDupCheckで重複レコードを無視しながら、CampaignNameからのレコードのみを必要とするため、NOT EXISTSを使用するようにコードを更新しました。更新後に0のレコードを与えるクエリの原因を教えてください。 FROM句にもテーブル名が含まれていますが、どこに問題があるのでしょうか – ross

関連する問題