Requirement: because the project has just been uploaded and there are no votes, in order to show a certain popularity, You need to assign a value to the number of votes for each project at the beginning,
However, each project cannot be the same; otherwise, the problem may be easily identified.
Copy codeThe Code is as follows:
DECLARE @ Id varchar (50)
DECLARE My_Cursor CURSOR -- defines the CURSOR
FOR (SELECT Id FROM dbo. kinpanAwardProject where session = 9) -- locate the desired set and put it in the cursor
OPEN My_Cursor; -- OPEN the cursor
Fetch next from My_Cursor INTO @ Id; -- read the first row of data (put the Id in the @ Id variable)
WHILE @ FETCH_STATUS = 0
BEGIN
PRINT @ Id; -- PRINT data (Id)
UPDATE dbo. kinpanAwardProject SET ProTicketCount = cast (floor (rand () * 30) as int) + 40 WHERE Id = @ Id; -- UPDATE data using a random number.
Fetch next from My_Cursor INTO @ Id; -- read the NEXT row of data (put the queried Id in the @ Id variable)
END
CLOSE My_Cursor; -- CLOSE the cursor
DEALLOCATE My_Cursor; -- release the cursor
GO
Another method may be better. In my opinion, if you do not need to judge the id, you do not need to query where when modifying the cursor. It is faster to directly modify the current position of the cursor.
Copy codeThe Code is as follows:
DECLARE My_Cursor CURSOR -- defines the CURSOR
FOR (SELECT * FROM dbo. MemberAccount) -- find the desired set and put it in the cursor.
OPEN My_Cursor; -- OPEN the cursor
Fetch next from My_Cursor; -- read the first row of data
WHILE @ FETCH_STATUS = 0
BEGIN
-- UPDATE dbo. MemberAccount SET UserName = UserName + 'A' where current of My_Cursor; -- UPDATE
-- Delete from dbo. MemberAccount where current of My_Cursor; -- DELETE
Fetch next from My_Cursor; -- read the NEXT row of data
END
CLOSE My_Cursor; -- CLOSE the cursor
DEALLOCATE My_Cursor; -- release the cursor
GO