执行存储过程来填充表列?

时间:2015-08-18 09:12:35

标签: sql-server

我可以使用EXEC关键字运行存储过程来填充QuestionScore列吗?

CREATE TABLE Question_Result 
(
     ResultID INT IDENTITY (10000, 1) NOT NULL,
     QResultID VARCHAR (10) NOT NULL DEFAULT 'QRES' + CAST(NEXT VALUE 
FOR non_identity_incrementer AS VARCHAR(10)),
     QuestionScore DECIMAL (4,2) EXEC dbo.update_question_result_question_score(),
     Assessment INT  NULL,
     DateTimeModified DATETIME NOT NULL DEFAULT SYSDATETIME(),

     PRIMARY KEY CLUSTERED (ResultID),
     FOREIGN KEY (Assessment) REFERENCES Assessment_Result (ResultID)
);

我的存储过程是:

CREATE PROCEDURE update_question_result_question_score (
    @QuestionScore DECIMAL(4,2) OUTPUT,
    @StudentAnswer NVARCHAR(MAX) OUTPUT,
    @AssessingAnswer NVARCHAR(MAX) OUTPUT
)
AS
BEGIN
    SELECT *  
    FROM StudentAnswerQuestionScore

    SET @QuestionScore = (SELECT Score FROM StudentAnswerQuestionScore) 
    SET @StudentAnswer = (SELECT [Student Answer] FROM StudentAnswerQuestionScore) 
    SET @AssessingAnswer = (SELECT [Assessing Answer] FROM StudentAnswerQuestionScore)

    IF (@StudentAnswer = @AssessingAnswer) 
       INSERT INTO Question_Result (QuestionScore) 
       VALUES (@QuestionScore) 
END

1 个答案:

答案 0 :(得分:0)

您可以尝试使用计算列的函数。希望下面的代码可以给你一些想法:

fragments