Amateur programming SQL Programming Learning--1 (SQL Server R2)

Source: Internet
Author: User

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

Related Article

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.