我试图记录未点击产品的次数。我已经有一个存储过程,可以从数据库中选择产品的详细信息。 我想在存储过程选择记录时更新其viewcount列。
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
我已经尝试过此代码将视图计数器更新为4。每次记录在视图计数器列中访问其增加的4。
我需要选择产品记录,并根据产品ID更新视图数。
答案 0 :(得分:1)
您不需要为此的子查询。只需更新值:
update [dbo].[Weportal_Product]
set viewcount = viewcount + 1
where ProductId = @iProductId;
答案 1 :(得分:0)
您可以尝试这个。
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