2013-10-06 64 views
0

我寫了這個Sp來插入/更新UserExams表,其中包含(UserID,ExamID,studentMark) 我需要爲沒有標記的學生添加新標記 並更新學生標記if它已經存在 我使用分割該函數返回表包含(ID,VITEM)更新和選擇忽略重複

我現在不哪裏是錯誤在這裏任何人可以幫助我,請 存在的錯誤,我得到的是:多部分組成的標識符「UserExams。用戶ID「不能被綁定。

ALTER PROC [dbo].[InsertUserMarks] 
( 
@pSemesterID int,  
@pCourseID int,  
@pExamID int,  
@pClassID int,  
@pUserID varchar(8000), 
@pMarks varchar(8000)  
)  
as  
--save the values of MaxMark To check if there is any Mark higher than the full mark if so then stop execution  
DECLARE @vTestMaxMark decimal(5,2)  
SET @vTestMaxMark = (select ExamMark from Exams where [email protected])  


IF EXISTS (select 'true' from Split(@pMarks,',') WHERE vItem>@vTestMaxMark)  
begin  
print('Operation cannot complete; there are one or more Mark ABOVE the Max value')  
return 
end  


      update UserExams 
      Set StudentMark=MA.vItem 
      from split(@pUserID,',') us 
      inner join split(@pMarks,',') ma on us.ID = ma.id 
      where UserExams.UserID=US.vItem 
      and [email protected] 


      --Insert 
      insert into UserExams 
      select us.vItem,1,ma.vItem,system_user,getdate(),null,null from 
      split(@pUserID,',') us 
      inner join split(@pMarks,',') ma on us.ID = ma.id 
      WHERE NOT EXISTS (
      SELECT 'True' 
      FROM split(@pUserID,',') us 
      inner join split(@pMarks,',') ma on us.ID = ma.id 
      where UserExams.UserID=US.vItem 
      and [email protected] 

      ) 

回答

0

該問題似乎在查詢的最底部。您可以使用引用UserExams表的子查詢插入,但該子查詢不包括其需要的FROM子句中的UserExams。

0

在你的情況下,我會利用合併聲明在這個過程中,所有的記錄在大表中存在可能是非常昂貴的。
合併聲明將檢查記錄的存在和利用當匹配然後更新當不匹配然後插入將使您的代碼更簡單,更容易理解和維護。