Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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,183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

721 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