Iterate over the current row of data and the addition of the previous row of data in SQL minus _mssql

Source: Internet
Author: User
Tags datetime rowcount

The following case, it is simpler to use the data in the cursor looping table #temptable, and then to add the current row and the argument1 in the previous line to the argument2 of the current row.

--drop table #temptable CREATE TABLE #temptable (argument1 int, argument2 int, argument3 datetime) DECLARE @rowc  ount int, @argument1 int, @argument2 nvarchar (m), @argument3 datetime set @rowcount =1 set @argument1 =1 set @argument2 =0 set @argument3 =getdate () while (@rowcount <100) begin inserts into #temptable (ARGUMENT1,ARGUMENT2,ARGUMENT3) value S (@argument1, @argument2, @argument3) Set @argument1 = @argument1 + DATEPART (day, @argument3) Set @argument3 = @argument3- 1 Set @rowcount = @rowcount + 1--select * from #temptable declare @lastargument2 int set @lastargument2 =0 SET @
Argument2=0 declare _cursor cursor for (select argument1 from #temptable) open _cursor; FETCH NEXT from _cursor to @argument2 while @ @fetch_status = 0 BEGIN update #temptable set argument2= @argumen t2+ @lastargument2 where CURRENT of the _cursor set @lastargument2 = @argument2 fetch NEXT from _cursor into @argument 2 End Close _cursor deallocate _cursor--select * from #teMptable

 

Ask a question:

The first fetch next from the _cursor into @argument2 why can't I put it in the first row of the while loop and delete the second line? I remember I was here in error, hehe.

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.