Solved

create loop using vbscript/asp to update values of recordset by using values of record 1 to update values of record 2

Posted on 2009-04-10
2
1,167 Views
Last Modified: 2012-05-06
I have an sql table with fields (id, value1, value2)

id value1 value 2
1  5         10
2  23       12
3  34       23
4  22       24

what i need is to create a vbscript of similar style to the one below which will run through a loop and for each row will proceed as follows:
1. get values of current row (which will use to update next row)
2. update values of current row with values taken from previous record
3. move to next row and get values of current row
4. update values of current row with values in step1
5. add new record in the end with last data

the resulting values of table must be after loop:

id value1 value2
1  5        10
2  5        10
3  23      12
4 34        23
5 22        24

   i = 0
    dim old1, old2
    old2 = 100
    mysql2 = "SELECT value1, value2 FROM table1"
      set rstemp2 = Server.CreateObject("ADODB.Recordset")
      rstemp2.Open mysql2, conn, 1,2
      do until rstemp2.eof
      old1 = rstemp2.fields("value1").value
      rstemp2.movenext
      i = i + 1
      loop
      rstemp2.close
      set rstemp2 = nothing
0
Comment
Question by:fzaza
2 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
ID: 24120353

All you need is incremnt the ID by 1 and insert a record with minimum id. Why can't you try like this.
The LOOP is slower than the DML statements. So use UPDATE/INSERT statement to do this.
Let me know if you still looking for loop.

create table #temp (id int,value1 int,value2 int)

insert #temp values (1,5,10),(2,23,12),(3,34,23),(4,22,24)

update #temp set id = id + 1 

insert #temp select id-1,value1,value2 from #temp where id = (select MIN(id) from #temp)

select * from #temp order by 1

/*

id	value1	value2

1	5	10

2	5	10

3	23	12

4	34	23

5	22	24

*/ 

drop table #temp

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now