Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
1,186 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
1 Comment
 
LVL 41

Accepted Solution

by:
Sharath earned 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

877 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