2013-07-19 101 views

回答

2

嘗試這一個 -

ALTER PROCEDURE dbo.getstudents 

     @year INT 

AS 
BEGIN 

    SELECT TOP 25 
      student_name 
      , student_id 
      , marks = SUM(marks) 
    FROM dbo.tbl_Students 
    WHERE [Year] = @year 
    GROUP BY 
      student_name 
      , student_id 
    ORDER BY marks DESC 

END 

或者這 -

ALTER PROCEDURE dbo.getstudents 

     @year INT 
    , @count INT 

AS 
BEGIN 

    SELECT student_name, student_id, marks 
    FROM (
     SELECT 
       student_name 
       , student_id 
       , marks 
       , rn = ROW_NUMBER OVER (ORDER BY marks DESC) 
     FROM (
      SELECT 
         student_name 
        , student_id 
        , marks = SUM(marks) 
      FROM dbo.tbl_Students 
      WHERE [Year] = @year 
       GROUP BY 
         student_name 
        , student_id 
     ) t 
    ) t2 
    WHERE t2.rn <= @count 

END 
+0

無效的列名。 – kvs

+1

對不起,我沒有水晶球。請爲您的桌子提供一個「DDL」。 – Devart

1

你以正確的方式傳遞的參數。現在

必須cange裏面你想要的信息SP查詢。

所以最後選擇顯示你已經提取出的結果。

顯然對經銷商的學生,學生的我等,你在這裏寫你的數據庫模型。

2

試試這個,對於今年的錯誤我得到

ALTER PROCEDURE getstudents 

     @year INT 

AS 
BEGIN 

    SELECT TOP 25 [student-name] 
    FROM tbl_Students WHERE [email protected] 

END 
相關問題