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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

867 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

16 Experts available now in Live!

Get 1:1 Help Now