2013-06-25 145 views
0

我收到提示在此存儲過程:這個SQL過程有什麼問題?

ALTER PROCEDURE [dbo].[spc_InsertSubjectToContentRelation] 
(
    @pCourseGUID NVARCHAR(MAX), 
    @pAssessmentIds NVARCHAR(MAX), 
    @pSubjectId NVARCHAR(MAX), 
    @pVideoIds NVARCHAR(MAX) 
) 
AS 
BEGIN 
SET NOCOUNT ON; 

IF EXISTS (SELECT * FROM SubjectToAssessmentAndVideoRelation WHERE SubjectId = @pSubjectId AND CourseGuid = @pCourseGuid) 
    UPDATE SubjectToAssessmentAndVideoRelation SET (AssociatedAssessments = @pAssessmentIds, AssociatedVideos = @pVideoIds) WHERE SubjectId = @pSubjectId AND CourseGuid = @pCourseGuid 
ELSE 
    INSERT INTO SubjectToAssessmentAndVideoRelation VALUES (@pCourseGUID, @pAssessmentIds, @pSubjectId, @pVideoIds) 
END 

的錯誤是:

過程spc_InsertSubjectToContentRelation,14號線
附近有語法錯誤('

什麼在這個查詢中是錯誤的嗎?

編輯:

我忘了在發佈代碼時加入EXISTS。我的實際代碼是存在的。我得到的誤差存在

回答

2

IF聲明沒有做任何事情,它僅包含一個表達式:

IF (SELECT * FROM SubjectToAssessmentAndVideoRelation WHERE SubjectId = @pSubjectId AND CourseGuid = @pCourseGuid) 

它必須是這樣的:

IF (SELECT * FROM SubjectToAssessmentAndVideoRelation WHERE SubjectId = @pSubjectId AND CourseGuid = @pCourseGuid) = 0 

--Then做你的邏輯

你可以改爲像這樣使用IF EXISTS

IF EXISTS (SELECT * FROM SubjectToAssessmentAndVideoRelation WHERE SubjectId = @pSubjectId AND CourseGuid = @pCourseGuid) 

希望有幫助。

+0

我改成了'IF EXISTS(SELECT * FROM SubjectToAssessmentAndVideoRelation WHERE SubjectId = @pSubjectId AND CourseGuid = @pCourseGuid)',但它沒有奏效。我得到相同的錯誤 – Jaguar

+0

道歉,我沒有注意到你的UPDATE語句的括號。您需要刪除(和)您輸入SET子句的位置。 – JBond

+0

謝謝,刪除大括號解決了我的問題:) – Jaguar

2
IF (SELECT * FROM SubjectToAssessmentAndVideoRelation WHERE SubjectId = @pSubjectId AND CourseGuid = @pCourseGuid) 

這不是有效的測試。你的意思是IF EXISTS(...

+0

我改成了'IF EXISTS(SELECT * FROM SubjectToAssessmentAndVideoRelation WHERE SubjectId = @pSubjectId AND CourseGuid = @pCourseGuid)',但它沒有奏效。我收到了同樣的錯誤 – Jaguar