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,172 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 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Access populate a field based on 2 other form fields 14 36
SQL- GROUP BY 4 25
SQL Server Express automatically execute SQL or SP 8 37
Append Query Access 2010 4 26
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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.

726 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