SQL UPDATE保存表达式不更新

时间:2014-10-15 15:27:11

标签: sql sql-server

我的存储过程中应该有一个非常简单的语句来将值递增+10。但是,如果我多次运行SP,则在第一次调用后该值不会更改。当我将增量值修改为+20时,结果表值是表中设置的ORIGINAL值的+20。

USE [DB]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spUpdateCareerRankFromInterviewByID](
    -- Add the parameters for the stored procedure here
    @studentID int
    )
AS
BEGIN

    DELETE FROM StudentCategoryRankTable
    WHERE   studentID = @studentID

    INSERT INTO StudentCategoryRankTable (studentID, occupationalCategoryID, categoryWeightedTotal)
    SELECT studentID, occupationalCategoryID, SUM(weightedResponse) AS weightedResponseTotal
    FROM            dbo.weightedInterviewResponsesView
    GROUP BY studentID, occupationalCategoryID
    HAVING        (studentID = @studentID)
    ORDER BY weightedResponseTotal DESC

    -- Determine whether the student only wants to pursue
    DECLARE @certificateOnlyQuestionOptionID INTEGER = 250
    DECLARE @highMathQuestionOptionID INTEGER = 43
    DECLARE @highReadingQuestionOptionID INTEGER = 47
    DECLARE @highWritingQuestionOptionID INTEGER = 51

    DECLARE @certificateOnlyOptionAnswered INTEGER = NULL
    DECLARE @highMathQuestionOptionAnswered INTEGER = NULL
    DECLARE @highReadingQuestionOptionAnswered INTEGER = NULL
    DECLARE @highWritingQuestionOptionAnswered INTEGER = NULL

    -- Returns 0 if the 
    EXEC @certificateOnlyOptionAnswered = dbo.choseQuestionOptionID @studentID = @studentID, @questionOptionID = @certificateOnlyQuestionOptionID
    EXEC @highMathQuestionOptionAnswered = dbo.choseQuestionOptionID @studentID = @studentID, @questionOptionID = @highMathQuestionOptionID
    EXEC @highReadingQuestionOptionAnswered = dbo.choseQuestionOptionID @studentID = @studentID, @questionOptionID = @highReadingQuestionOptionID
    EXEC @highWritingQuestionOptionAnswered = dbo.choseQuestionOptionID @studentID = @studentID, @questionOptionID = @highWritingQuestionOptionID


    -- Determine if the student only wants a certificate / non-degree
    IF @certificateOnlyOptionAnswered != 0
    BEGIN
        INSERT INTO dbo.TesterTable (TestColumn) VALUES ('Certificate True')

        -- High Math, add weight to scores for
            --  3: Business
            --  5: Computer Technologies
            --  7: Engineering Technology
            -- 18: Sciences and Math
        IF @highMathQuestionOptionAnswered != 0
        BEGIN
            INSERT INTO dbo.TesterTable (TestColumn) VALUES ('High Math')

            UPDATE  dbo.StudentCategoryRankTable
            SET     categoryWeightedTotal += 10
            WHERE   studentID = @studentID AND
                        occupationalCategoryID IN (3, 5, 7, 18)
        END

        -- High Reading/Writing, add weight to scores for
            --  4: Communications
            -- 10: Humanities
            -- 19: Social and Behavioral Sciences
        IF @highReadingQuestionOptionAnswered != 0 OR @highWritingQuestionOptionAnswered != 0
        BEGIN
            INSERT INTO dbo.TesterTable (TestColumn) VALUES ('High Reading/Writing')

            UPDATE  dbo.StudentCategoryRankTable
            SET     categoryWeightedTotal += 10
            WHERE   studentID = @studentID AND
                        (occupationalCategoryID = 4 OR
                         occupationalCategoryID = 10 OR
                         occupationalCategoryID = 19)
        END
    END
    ELSE
    BEGIN
        INSERT INTO dbo.TesterTable (TestColumn) VALUES ('Failed Certificate Only')
    END
END

1 个答案:

答案 0 :(得分:4)

您首先发布的程序将删除StudentCategoryRankTable中的所有条目,然后插入新条目。这些新记录已更新。如果再次运行该程序,它将DELETE更新的记录,您将返回到开始的位置。