这个SQL程序有什么问题?

时间:2013-06-25 08:07:49

标签: sql-server-2008

我在这个存储过程中遇到错误:

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 个答案:

答案 0 :(得分:2)

您的IF语句没有做任何事情,它只包含一个表达式:

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

它必须是:

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

- 然后做你的逻辑

您可以使用IF EXISTS

来完成此类操作
IF  EXISTS (SELECT * FROM SubjectToAssessmentAndVideoRelation WHERE SubjectId = @pSubjectId AND CourseGuid = @pCourseGuid)

希望有所帮助。

答案 1 :(得分:2)

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

这不是有效的测试。您的意思是IF EXISTS(...吗?