I am trying to record no of click on Product.I already have a stored procedure that selects the details of products from DB. I want to update the viewcount column when the stored procedure whenever it selects the record.
ALTER PROCEDURE [dbo].[Weportal_sp_Product_SelectOne]
@iProductId int,
@iErrorCode int OUTPUT
AS
SET NOCOUNT ON
-- SELECT an existing row from the table.
update [dbo].[Weportal_Product] set viewcount =((select viewcount from [dbo].[Weportal_Product] where ProductId=@iProductId)+1) where ProductId=@iProductId;
SELECT * FROM [dbo].[Weportal_Product]
WHERE ProductId = @iProductId
-- Get the Error Code for the statement just executed.
SELECT @iErrorCode=@@ERROR
I already tried this code update the view counter but by four. Each time the record accessed its increase 4 in view counter column.
I need to select the product record and update the number of views against the products id.
You don't need a subquery for this. Just update the value:
update [dbo].[Weportal_Product]
set viewcount = viewcount + 1
where ProductId = @iProductId;
You may try this.
update WP SET WP.VIEWCOUNT = P.VIEWCOUNT + 1
FROM [dbo].[Weportal_Product] AS WP
INNER JOIN
(SELECT ProductId, MAX(VIEWCOUNT) AS VIEWCOUNT
FROM [dbo].[Weportal_Product] GROUP BY ProductId) AS P
ON WP.ProductId = P.ProductId --- you may apply further conditions over there to filter out the results
WHERE WP.ProductId = @iProductId
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.