不存在不能很好地使用多列

时间:2016-03-18 19:38:30

标签: sql-server exists

这一定是我刚刚错过的简单......

我有临时表说:

CREATE TABLE #tsa
(
    AttendeeID int,
    SurveyID int,
    TrainingAttendeeID int
)

我使用TOP 1得到一条与此类似的记录:

SELECT
        TOP 1
        @AttendeeID=ta.AttendeeID,
        @SurveyID=ts.SurveyID,
        @TrainingAttendeeID = ta.TrainingAttendeeID
    FROM            
        TrainingAttendee ta
    INNER JOIN
        [User] u
    ON
        u.UserID= ta.AttendeeID
    INNER JOIN
        [Training] t
    ON
        t.TrainingID = ta.TrainingID
    INNER JOIN
        [TrainingSet] ts
    ON
        ts.TrainingSetID = t.TrainingSetID
    WHERE
        ta.AttendedTraining = 1
        AND ta.ConfirmAttendedEmailOn IS NOT NULL
        --only get people who didn't fill out the survey
        AND NOT EXISTS (SELECT * FROM SurveyTaken st WHERE st.AddedByUserID = ta.AttendeeID AND st.SurveyID = ts.SurveyID)
    ORDER BY
        ta.AttendeeID,
        ts.SurveyID

一旦我得到这条记录,我就把它存储到我的临时表中:

--insert into our temp table
INSERT INTO #tsa(AttendeeID, SurveyID, TrainingAttendeeID) 
VALUES(@AttendeeID, @SurveyID, @TrainingAttendeeID)

然后我需要经历检查一些数据和发送电子邮件的整个过程......一旦发送电子邮件,我需要拿起下一条记录,不包括我以前的记录......所以没有显示太多代码:

WHILE SomeCondition
BEGIN
  --do some thing...

  --pick up next one
--grab next one
                SELECT
                    TOP 1
                    @AttendeeID = ta.AttendeeID,
                    @SurveyID=ts.SurveyID,
                    @TrainingAttendeeID=ta.TrainingAttendeeID
                FROM            
                    TrainingAttendee ta
                INNER JOIN
                    [User] u
                ON
                    u.UserID= ta.AttendeeID
                INNER JOIN
                    [Training] t
                ON
                    t.TrainingID = ta.TrainingID
                INNER JOIN
                    [TrainingSet] ts
                ON
                    ts.TrainingSetID = t.TrainingSetID
                WHERE
                    (
                        --same where as original
                        ta.AttendedTraining = 1
                        AND (ta.ConfirmAttendedEmailOn IS NOT NULL)
                        AND (NOT EXISTS (SELECT * FROM SurveyTaken st WHERE st.AddedByUserID = ta.AttendeeID AND st.SurveyID = ts.SurveyID))
                        --adding the piece such that we compare against the temp table...
                        AND (NOT EXISTS (SELECT * FROM #tsa tempS WHERE tempS.AttendeeID = ta.AttendeeID AND tempS.SurveyID = ts.SurveyID AND tempS.TrainingAttendeeID = ta.TrainingAttendeeID))
                    )
                ORDER BY
                    ta.AttendeeID,
                    ts.SurveyID

                --insert into our temp table    
                INSERT INTO #tsa(AttendeeID, SurveyID, TrainingAttendeeID) 
                VALUES(@AttendeeID, @SurveyID, @TrainingAttendeeID)
END

注意这个内部的条件......我又添加了一个AND ...即:

--adding the piece such that we compare against the temp table...
AND (NOT EXISTS (SELECT * FROM #tSa tempS WHERE tempS.AttendeeID = ta.AttendeeID AND tempS.SurveyID = ts.SurveyID AND tempS.TrainingAttendeeID = ta.TrainingAttendeeID))

为了确保我不会重复使用我已经在临时表中处理的记录...而且你会注意到我最后也会重新插入我的临时表...

--insert into our temp table    
INSERT INTO #tsa(AttendeeID, SurveyID, TrainingAttendeeID) 
VALUES(@AttendeeID, @SurveyID, @TrainingAttendeeID)

每次我运行这个存储过程都会无限期地继续下去,所以我认为此时我的病情有问题。我有一个大脑放屁......或许办公室里有太多的噪音。我在这里错过了什么?我放置了一个print语句,它继续处理相同的记录......所以有些东西告诉我where子句中的最后一个条件是不正确的。

修改

这里是整个程序......我的问题是记录集中只有一条记录......但是sproc继续处理同样的记录

PROCEDURE ScriptSendTrainingSurveyReminders 
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @AttendeeID int
    DECLARE @TrainingAttendeeID int
    DECLARE @SurveyID int
    DECLARE @Message nvarchar(MAX)
    DECLARE @Subject nvarchar(255)

        CREATE TABLE #tSa
        (
            AttendeeID int,
            SurveyID int,
            TrainingAttendeeID int
        )

    SELECT
        TOP 1
        @AttendeeID=ta.AttendeeID,
        @SurveyID=ts.SurveyID,
        @TrainingAttendeeID = ta.TrainingAttendeeID
    FROM            
        TrainingAttendee ta
    INNER JOIN
        [User] u
    ON
        u.UserID= ta.AttendeeID
    INNER JOIN
        [Training] t
    ON
        t.TrainingID = ta.TrainingID
    INNER JOIN
        [TrainingSet] ts
    ON
        ts.TrainingSetID = t.TrainingSetID
    WHERE
        ta.AttendedTraining = 1
        AND ta.ConfirmAttendedEmailOn IS NOT NULL
        --only get people who didn't fill out the survey
        AND NOT EXISTS (SELECT * FROM SurveyTaken st WHERE st.AddedByUserID = ta.AttendeeID AND st.SurveyID = ts.SurveyID)
    ORDER BY
        ta.TrainingAttendeeID,
        ta.AttendeeID,
        ts.SurveyID

    --insert into our temp table
    INSERT INTO #tSa(AttendeeID, SurveyID, TrainingAttendeeID) 
    VALUES(@AttendeeID, @SurveyID, @TrainingAttendeeID)

    WHILE @TrainingAttendeeID IS NOT NULL AND @AttendeeID IS NOT NULL AND @SurveyID IS NOT NULL
        BEGIN
            DECLARE @TrainingID int
            DECLARE @Title nvarchar(50)
            DECLARE @StartDateTime nvarchar(50)
            DECLARE @EndDateTime nvarchar(50)
            DECLARE @FullName nvarchar(255)
            DECLARE @EmailAddress nvarchar(255)
            DECLARE @Description nvarchar(MAX)

            --get the one record we are on...
            SELECT 
                @TrainingID = t.TrainingID,
                @Title = t.Title,
                @StartDateTime = CAST(CONVERT(DATE, t.StartDate) AS VARCHAR(50)) + ' ' + CAST(t.StartTimeHours AS VARCHAR(50)) + ':' + CAST(CASE WHEN LEN(t.StartTimeMinutes)=1 THEN  CAST(t.StartTimeMinutes AS VARCHAR(50)) + '0' ELSE CAST(t.StartTimeMinutes AS VARCHAR(50)) END AS VARCHAR(50)) + ' ' + CAST(t.StartTimeAMorPM AS VARCHAR(50)),
                @EndDateTime = CAST(CONVERT(DATE, t.EndDate) AS VARCHAR(50)) + ' ' + CAST(t.EndTimeHours AS VARCHAR(50)) + ':' + CAST(CASE WHEN LEN(t.EndTimeMinutes)=1 THEN  CAST(t.EndTimeMinutes AS VARCHAR(50)) + '0' ELSE CAST(t.EndTimeMinutes AS VARCHAR(50)) END AS VARCHAR(50)) + ' ' + CAST(t.EndTimeAMorPM AS VARCHAR(50)),
                @Description = t.DescriptionHTML,
                @FullName = u.FullName,
                @EmailAddress = u.EmailAddress
            FROM 
                Training t 
            INNER JOIN
                TrainingAttendee ta
            ON
                t.TrainingID = ta.TrainingID
            INNER JOIN
                [User] u
            ON
                u.UserID = ta.AttendeeID
            WHERE
                ta.TrainingAttendeeID = @TrainingAttendeeID

            IF @EmailAddress IS NOT NULL
                BEGIN 
                      --Email goes out here....
                END

            --grab next one
                SELECT
                    TOP 1
                    @AttendeeID = ta.AttendeeID,
                    @SurveyID=ts.SurveyID,
                    @TrainingAttendeeID=ta.TrainingAttendeeID
                FROM            
                    TrainingAttendee ta
                INNER JOIN
                    [User] u
                ON
                    u.UserID= ta.AttendeeID
                INNER JOIN
                    [Training] t
                ON
                    t.TrainingID = ta.TrainingID
                INNER JOIN
                    [TrainingSet] ts
                ON
                    ts.TrainingSetID = t.TrainingSetID
                WHERE
                    (
                        --same where as original
                        (ta.AttendedTraining = 1)
                        AND (ta.ConfirmAttendedEmailOn IS NOT NULL)
                        AND (NOT EXISTS (SELECT * FROM SurveyTaken st WHERE st.AddedByUserID = ta.AttendeeID AND st.SurveyID = ts.SurveyID))
                        --adding the piece such that we compare against the temp table...
                        AND (NOT EXISTS (SELECT * FROM #tSa tempS WHERE tempS.AttendeeID = ta.AttendeeID AND tempS.SurveyID = ts.SurveyID AND tempS.TrainingAttendeeID = ta.TrainingAttendeeID))
                    )
                ORDER BY
                    ta.TrainingAttendeeID,
                    ta.AttendeeID,
                    ts.SurveyID

                PRINT CAST('TrainingAttendeeID: ' + CAST(@TrainingAttendeeID as nvarchar(500)) + ' AttendeeID:' + CAST(@AttendeeID as nvarchar(500)) + ' SurveyID: ' + CAST(@SurveyID as nvarchar(500)) AS nvarchar(4000))
                --insert into our temp table    
                INSERT INTO #tSa(AttendeeID, SurveyID, TrainingAttendeeID) 
                VALUES(@AttendeeID, @SurveyID, @TrainingAttendeeID)
        END
END
GO

1 个答案:

答案 0 :(得分:2)

如果select不返回任何记录,变量将不会更改。我打赌它会绕过最后的@AttendeeID。

测试它的另一种方法 - 为临时表添加唯一约束。一旦没有更多的记录可供选择,我认为周期将失败。

解决此问题的一种方法 - 在每次迭代开始时为所有变量分配NULL (位于{{1}}正文的顶部)。但我建议尽可能将此代码重写为游标(不确定几个select语句的逻辑是什么)。

请注意,代码块中的变量声明不会产生"块范围"感觉因为它不是perl或python。