2012-08-10 211 views
2

我有一個存儲過程按分數來獲得一個療程的GPA乘以信貸的結果:調用存儲在另一個存儲過程的過程

ALTER PROCEDURE dbo.GpaCreditFromScore 
(
    @score INT, 
    @credits INT, 
    @gpaCredit INT OUTPUT 
) 
AS 
IF (@score < 60) 
BEGIN 
    SET @gpaCredit = 0.0 
END 
ELSE IF (@score < 62) 
BEGIN 
    SET @gpaCredit = 1.0 * @credits 
END 
ELSE IF (@score < 65) 
BEGIN 
    SET @gpaCredit = 1.7 * @credits 
END 
ELSE IF (@score < 67) 
BEGIN 
    SET @gpaCredit = 2.0 * @credits 
END 
ELSE IF (@score < 70) 
BEGIN 
    SET @gpaCredit = 2.3 * @credits 
END 
ELSE IF (@score < 75) 
BEGIN 
    SET @gpaCredit = 2.7 * @credits 
END 
ELSE IF (@score < 80) 
BEGIN 
    SET @gpaCredit = 3.0 * @credits 
END 
ELSE IF (@score < 85) 
BEGIN 
    SET @gpaCredit = 3.3 * @credits 
END 
ELSE IF (@score < 90) 
BEGIN 
    SET @gpaCredit = 3.7 * @credits 
END 
ELSE IF (@score < 95) 
BEGIN 
    SET @gpaCredit = 4.0 * @credits 
END 
ELSE IF (@score <= 100) 
BEGIN 
    SET @gpaCredit = 4.3 * @credits 
END 

而另一個存儲過程來計算所有學生的GPA乘信用:

ALTER PROCEDURE dbo.ComputeGpa 
AS 
    ;WITH x AS 
(
    SELECT StudentID, Score, rn = ROW_NUMBER() OVER 
    (PARTITION BY StudentID, CourseID 
    ORDER BY Semester DESC) 
    FROM dbo.UndergraduateScoreSet 
) 
SELECT StudentID, Gpa /* call dbo.GpaCreditFromScore here to get @gpaCredit */ 
FROM x 
WHERE rn = 1 
GROUP BY StudentID; 

由於邏輯非常複雜,我不知道如何實現這一點。請大家幫忙〜

+3

如果你有一個表列'LowerScoreBound','UpperScoreBound '和'乘數',你可以很容易地獲得乘數作爲一個基於集的操作。然後,你可以大大簡化你的存儲過程(這應該是一個UDF)或完全消除它。 – 2012-08-10 06:19:44

+0

與其編輯您的問題以包含答案,您應該將答案發布爲...答案(然後請回滾您的編輯)。 – 2012-08-10 07:24:18

回答

2

不幸的是,因爲proc dbo.GpaCreditFromScore接受並返回了標量參數,這意味着你必須通過遍歷你的學生來計算所有分數,通過使用像遊標一樣的可怕東西。

由於@Damien提到,你應該改變dbo.GpaCreditFromScore是一個用戶定義的函數,像這樣:

CREATE FUNCTION dbo.GpaCreditFromScore 
(
    @score INT, 
    @credits INT 
) 
RETURNS INT 
AS 
    BEGIN 
     DECLARE @gpaCredit INT 
     IF (@score < 60) 
     BEGIN 
      SET @gpaCredit = 0.0 
     END 
     ELSE IF (@score < 62) 
     BEGIN 
      SET @gpaCredit = 1.0 * @credits 
     END 
     ELSE IF (@score < 65) 
     BEGIN 
      SET @gpaCredit = 1.7 * @credits 
     END 
     ELSE IF (@score < 67) 
     BEGIN 
      SET @gpaCredit = 2.0 * @credits 
     END 
     ELSE IF (@score < 70) 
     BEGIN 
      SET @gpaCredit = 2.3 * @credits 
     END 
     ELSE IF (@score < 75) 
     BEGIN 
      SET @gpaCredit = 2.7 * @credits 
     END 
     ELSE IF (@score < 80) 
     BEGIN 
      SET @gpaCredit = 3.0 * @credits 
     END 
     ELSE IF (@score < 85) 
     BEGIN 
      SET @gpaCredit = 3.3 * @credits 
     END 
     ELSE IF (@score < 90) 
     BEGIN 
      SET @gpaCredit = 3.7 * @credits 
     END 
     ELSE IF (@score < 95) 
     BEGIN 
      SET @gpaCredit = 4.0 * @credits 
     END 
     ELSE IF (@score <= 100) 
     BEGIN 
      SET @gpaCredit = 4.3 * @credits 
     END 
     RETURN @gpaCredit 
    END 

你的存儲過程並不完全工作(?哪裏學分來自),但這個想法是現在使用UDF你的CTE的領域,像這樣:

ALTER PROCEDURE dbo.ComputeGpa 
AS 
    ;WITH x AS 
    (
     SELECT StudentID, Score, 9 AS Credits, rn = ROW_NUMBER() OVER 
     (PARTITION BY StudentID, CourseID 
     ORDER BY Semester DESC) 
     FROM dbo.UndergraduateScoreSet 
    ) 
    SELECT StudentID, dbo.GpaCreditFromScore(Score, Credits) AS GpaCredit 
    FROM x 
    WHERE rn = 1 
    GROUP BY StudentID; 

編輯 OP發現錯誤 - 因爲GROUP BY的,我們需要一個總的,當然。謝謝!:

SELECT StudentID, SUM(dbo.GpaCreditFromScore(Score, Credits)) AS GpaCredit 
+1

正如我在問題的結尾提到的那樣。但是你的回答真的很有幫助!非常感謝! – Ovilia 2012-08-10 07:21:39

相關問題