|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Trying to update index for each field...
Hello,
I have a DB that has a lot of double users and junk users. I want to delete the extras and renumber the index. I was trying to use a for each loop and movenext() but I don't know how to use them correctly in an update. The table name is student, and the field I want to update is ID. if anyone can help, I would really appreciate it. Thanks, Krs |
|
#2
|
||||
|
||||
|
What database are you using?
What's the data type of the ID field in the database? |
|
#3
|
||||
|
||||
|
The database is SQL and the type is 'int'.
I've tried so many different versions of code, but here is one. Code:
strSQL= "select id from students"
newStrSQL= "update students SET id ='" & indexNum & "'"
SET rs1 = Server.CreateObject("ADODB.Connection")
rs1.Open = MM_RU_STRING
rs1.Execute (strSQL)
while not rs1.eof
indexNum = indexNum + 1
rs1.Execute (newStrSQL)
rs1.movenext()
wend
I fogot ot include the error I get. |21|800a0bb9|Arguments_are_of_the_wrong_type__are_ out_of_acceptable_range__or_are_in_conflict_with_o ne_another. |
|
#4
|
||||
|
||||
|
1) SQL isn't a database, it's a language and it stands for (Structured Query Language).
SQL Server is a database. 2) Your code is wrong for a couple of reasons A) strings (varchar, etc...) are the only types the need to be surrounded by single quotes (') So this code Code:
newStrSQL= "update students SET id ='" & indexNum & "'" Should be like this, since id is an int (number) type and not a string. Code:
newStrSQL= "UPDATE students Set id = " & indexNum This is how I would do it Code:
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open MM_RU_STRING
strSql = "SELECT id FROM students"
Set rs = Conn.Execute(strSql)
i = 1
Do until rs.eof
Conn.Execute("UPDATE Students Set id = " & i & " WHERE id = " & rs("id"))
rs.movenext
i = i + 1
loop
|
|
#5
|
||||
|
||||
|
Quote:
Thanks, I know the difference, I just knew you would know what I meant. :-) Quote:
Ok, so since the indexNum was a variable, I didn't need the extra singe quote mark? I was never sure as to why I needed to use the single quote. was that the main problem? This is how I would do it Code:
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open MM_RU_STRING
strSql = "SELECT id FROM students"
Set rs = Conn.Execute(strSql)
i = 1
Do until rs.eof
Conn.Execute("UPDATE Students Set id = " & i & " WHERE id = " & rs("id"))
rs.movenext
i = i + 1
loop
[/QUOTE] Thanks it works great. You're the best!! |
|
#6
|
||||
|
||||
|
Review my signature regarding "Helpful Posts".
|
![]() |
| Viewing: ASP Free Forums > Database > SQL Development > Trying to update index for each field... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|