MSSQL database cursor batch changes to eligible records

Source: Internet
Author: User
Tags mssql rand

Requirements: Because the project has just uploaded, no votes, in order to show a certain popularity, it is necessary to start at the beginning of the number of votes to assign a value,

But every project can not be the same, otherwise easy to see the problem, hehe.


DECLARE @Id varchar (DECLARE) my_cursor cursor--Defines the cursor for (SELECT Id from Dbo.kinpanawardproject where session=9)--to isolate the required The set is placed in the cursor open my_cursor; --Open cursor fetch NEXT from my_cursor to @Id; --Reads the first row of data (puts the Id into 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 the data, using random numbers.        fetch NEXT from my_cursor into @Id;--Reads the next line of data (puts the Id of the query into the @Id variable)    Endclose My_cursor; --Close the cursor deallocate my_cursor; --Release cursor go


Another way to use it might be better, I think, assuming there is no need to infer the ID, there is no need for a where query when changing, and it is quicker to directly alter the cursor's current position.

From: http://www.cnblogs.com/zengen/archive/2011/08/10/2133423.html

DECLARE my_cursor Cursor--Defines the cursor for (SELECT * FROM dbo. Memberaccount)--to isolate the required set into the cursor open my_cursor; --Open the cursor fetch NEXT from my_cursor; --Reads 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    endclose my_cursor;--close cursor deallocate my_cursor;--Release cursor go



3. How to Query Multiple field values




DECLARE @Id varchar (50)
DECLARE @dtime datetime
DECLARE my_cursor Cursor--Defining cursors
For (SELECT id,updatetime from dbo. Propertypro)--Find the required set to cursor has an
OPEN My_cursor; --Open cursor
FETCH NEXT from My_cursor to @Id, @dtime; --Reads the first row of data (puts the ID into the @id variable, updatetime the value into the @dtime)
While @ @FETCH_STATUS = 0
BEGIN
--select @dtime = UpdateTime from propertypro where [email protected];
PRINT @Id + ' +cast (@dtime as varchar (30)); --Print data (ID)
Update propertyprofile set [email protected] where [email protected] and updatetime is not null;
--update dbo.kinpanawardproject SET proticketcount = CAST (Floor (rand () *20) as int) +70 WHERE Id = @Id; --Update data
FETCH NEXT from My_cursor to @Id, @dtime; --Reads the next line of data (puts the ID of the query into the @id variable)
END
CLOSE My_cursor; --Close cursor
Deallocate my_cursor; --Releasing cursors
GO


Condition inference re-operation:


DECLARE @Id varchar (50)
--declare @votecount int
DECLARE @votesum int
DECLARE @voteid varchar (100)

DECLARE my_cursor Cursor--Defining cursors
for (SELECT distinct pp. Id from dbo. Propertypro pp,propertycategory pc where Pc.id=pp.classid and (pc.parentid=2 or pc.parentid=3 or pc.parentid=4)--Isolate the required set Fit to Cursor has an
OPEN My_cursor; --Open cursor
FETCH NEXT from My_cursor to @Id; --Reads the first row of data (puts the ID into the @id variable)
While @ @FETCH_STATUS = 0
BEGIN
PRINT @Id; --Print data (ID)
Set @voteid =replace (replace (replace (varchar (), GETDATE (), 121), '-', '), ' ', '), ': ', '), '. ', ') + Left (replace (newid (), '-', '), 10)
--select @votecount =count (1) from uservote where [email protected]
Select @votesum =sum (tickets) from uservote where [email protected]--Query the total number of likes

If @votesum is null
Begin
Set @votesum =0
End
If @votesum <1200--The total number of likes is less than 1200 before you delete and initialize the number of likes
Begin
Delete from uservote where [email protected]
INSERT INTO dbo. Uservote (Id,userid,awardproid,votedatetime,tickets) VALUES (@voteid, ' 1 ', @Id, GETDATE (), CAST (Floor (rand () *1800) as int) +1200+ @votesum)
End

--UPDATE dbo.kinpanawardproject SET proticketcount = CAST (Floor (rand () *20) as int) +70 WHERE Id = @Id; --Update data
FETCH NEXT from My_cursor to @Id; --Reads the next line of data (puts the ID of the query into the @id variable)
END
CLOSE My_cursor; --Close cursor
Deallocate my_cursor; --Releasing cursors
GO
/*
Select top $ * from Uservote ORDER BY votedatetime Desc
*/





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.