UPDATE views count (topic, videos, posts) in SQL Server


Hello guys, as we required sometimes adding view of any topics, news and images how many times user has been seen. So for that purpose we just need to create one separate table as below

We need to create on table TopicViews with Three column name as

viewsId int auto_increment,

topicId int,

viewCount int DEFAULT 0 (zero)

After that you need to create a procedure as below. It’s finish.

CREATE PROCEDURE usp_AddUpdateViews
@topicId int

AS

BEGIN

DECLARE @Cntr AS int

SET @Cntr = (SELECT ViewCount FROM  TopicViews   WHERE topicId = @ topicId)

PRINT @Cntr
IF @Cntr > 0

      BEGIN
            UPDATE  TopicViews  SET ViewCount = @Cntr+1 WHERE topicId = @ topicId
      END

ELSE

      BEGIN
            INSERT INTO  TopicViews  (topicId,ViewCount) VALUES (@topicId,1)
      END

END

Comments