1, establish test database;
2, create test1 data sheet;
The Test1 table properties are as follows:
Where the ID field is set to identity increment, and the increment seed is 1;
This instance is implemented starting with the first row of data, adding the last field value of each row to the second field value minus the third field value, and updating the last field value of the current row with the new value obtained, looping until all records have been updated. (This code is the inspiration I got on MSDN, thanks to the people who helped me on MSDN, and your help made me a lot more progress.)
The script code is as follows:
Use[test]
GO
--Define all the required variables
[Email protected] Store the current ID number
[Email protected] store last ID number
[Email protected] holds ID identifier (exists as 1, does not exist as 0)
[Email protected] Store the calculated inventory number
DECLARE @i int, @Count int, @TempID int, @Cuna int
--Implement get last ID number
SELECT @Count =id from TEST1
--Read the ID number of the first record and save it to @i
SELECT TOP 1 @i=id from Test1
--Update the Cun field value in the first record
UPDATE TEST1 SET Cun=jin-chu WHERE [email protected]
--Read the updated Cun field value assignment to the TEMP variable @cuna
SELECT @Cuna =cun from TEST1 WHERE [email protected]
[email protected] self-increment 1
SET @[email protected]+1
--loop to determine if the last record is reached
while (@i<[email protected])
BEGIN
--Find records equal to ID and @i
SELECT @TempID =count (1) from TEST1 WHERE [email protected]
BEGIN
--record does not exist @i self-increment 1
IF (@TempID =0)
SET @[email protected]+1
ELSE
--Record of update record exists
UPDATE TEST1 SET [email protected]+jin-chu WHERE [email protected]
--Read the updated Cun field value
SELECT @Cuna =cun from TEST1 WHERE [email protected]
END
BEGIN
--Determine if the last record is reached
IF (@i> @Count)
--execution exit when the last record is reached
Break
ELSE
--@i increased by 1 to reach the last record
SET @[email protected]+1
--Continue to return while executing while loop body statement
CONTINUE
END
END