2016-07-19 120 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 
相關問題