在我的数据库中我有两个表项(Id,…,ToatlViews int)和ItemViews(id,ItemId,Timestamp)
在ItemViews表中,我存储项目进入站点时的所有视图.我不时想调用存储过程来更新Items.ToatlViews字段.我尝试使用游标执行此SP …但更新语句错误.你能帮我纠正一下吗?没有光标我可以这样做吗?
CREATE PROCEDURE UpdateItemsViews AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @currentItemId int DECLARE @currentItemCursor CURSOR SET @currentItemCursor = CURSOR FOR SELECT Id FROM dbo.Items OPEN @currentItemCursor FETCH NEXT FROM @currentItemCursor INTO @currentItemId WHILE @@FETCH_STATUS = 0 BEGIN Update dbo.Items set TotalViews = count(*) from dbo.ItemViews where ItemId=@currentItemId FETCH NEXT FROM @currentItemCursor INTO @currentItemId END END GO