2016-07-19 70 views
0
ALTER procedure [dbo].[proc_PostVisitCountInsert](
@postCount bigint, 
@postID bigint) 
as 
Begin 
    if not exists(select * from PostVisitCount where [email protected]) 
    Begin 
    Insert into PostVisitCount(postcount,postID) values(@postCount, @postID) 
    end 
    else 
    Begin 
    --Query for Update PostVisitCount 
     Declare @count bigint 
     Set @count=(select postcount from PostVisitCount where [email protected]) 
     Update PostVisitCount set postcount=(@count+1), [email protected] 
    End 
end --Error getting at this place 

当我尝试执行查询时,出现这些查询时出错。我不明白这个查询有什么问题,任何人都能帮我弄清楚我做错了什么。关键字'结束'附近的语法不正确。 sql

+1

请张贴整个查询,使一些人可以解析和帮助 – TheGameiswar

+0

你有更新查询吗,还是只有这条评论?开始和结束语句不能为空;声明需要在开始和结束之间。 – steenbergh

+0

@TheGameiswar:我添加更新查询到它不工作显示相同的错误 – user6503334

回答

-1

可能这应该起作用。

ALTER procedure [dbo].[proc_PostVisitCountInsert](
@postCount bigint, 
@postID bigint) 
as 
Begin 
    if not exists(select * from PostVisitCount where [email protected]) 

    Insert into PostVisitCount(postcount,postID) values(@postCount, @postID) 
    else 

    --Query for Update PostVisitCount 

end --Error getting at this place 
+0

开始和结束没有任何错误在第一个IF分支中,我不会有这样的空ELSE。 – steenbergh

1

ELSE的Begin End没有包含任何语句(它实际上被注释掉了)。这是错误的原因。下面的工作

ALTER procedure [dbo].[proc_PostVisitCountInsert](
@postCount bigint, 
@postID bigint) 
as 
Begin 
    if not exists(select * from PostVisitCount where [email protected]) 
    Begin 
    Insert into PostVisitCount(postcount,postID) values(@postCount, @postID) 
    end 
    else 
    Begin 
    Query for Update PostVisitCount 
    End 
end --Error getting at this place 
+0

如果是这种情况(我承认这也是我最初的想法),错误会在之前提出一行。 –

+0

你可以发布你使用的完整代码吗? – Madhivanan

+0

抱歉,该作品\ – TheGameiswar

0

@ user6503334:我不知道我的帖子是否会解决您的问题,但你可以改变,如下面的“其他人”的部分,因为我觉得有东西在你的更新语句逻辑缺失。我在其他部分做了一些改变。你应该添加where子句,否则所有的行都会被更新。

ALTER PROCEDURE [dbo].[proc_PostVisitCountInsert] (
    @postCount BIGINT 
    ,@postID BIGINT 
    ) 
AS 
BEGIN 
    IF NOT EXISTS (
      SELECT 1 
      FROM PostVisitCount 
      WHERE postID = @postID 
      ) 
    BEGIN 
     INSERT INTO PostVisitCount (
      postcount 
      ,postID 
      ) 
     VALUES (
      @postCount 
      ,@postID 
      ) 
    END 
    ELSE 
    BEGIN 
     --Query for Update PostVisitCount 
     DECLARE @count BIGINT 

     SET @count = (
       SELECT sum(postcount) + 1 
       FROM PostVisitCount 
       WHERE postID = @postID 
       ) 

     UPDATE PostVisitCount 
     SET postcount = @count 
     WHERE postID = @postID 
    END 
END 
0

因为一个有效的答案在评论给出,不是每个人都读那些脚本应该是这样的:

ALTER procedure [dbo].[proc_PostVisitCountInsert](
    @postCount bigint, 
    @postID bigint 
) 
as 
Begin 
    if not exists (select * 
     from PostVisitCount 
     where postId = @postId) 
    Begin 
     insert into PostVisitCount(postcount,postID) values(@postCount, @postID) 
    end   
    else 
    Begin 
     --Query for Update PostVisitCount 
     update PostVisitCount set postcount = postcount + 1 where postID = @postID 
    End 
end 
相关问题