|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi,
I have a string --> 7,8,9,10. This string may contain more or less than 4 numbers. I want to separate each number and run an sql query for each number. for example: select * from table1 where code = '7' select * from table1 where code ='8' and etc.... Can I do this in a loop? Any suggestions will be appreciated!! Thanks ![]() |
|
#2
|
|||
|
|||
|
Something like this should work (untested!)
Code:
dim n1, n2, s1 n1 = 1 do n2 = instr(mystr, n1) if n2 < n1 then exit do 'no more pieces s1 = mid(mystr, n1, n2) 'you may need to adjust +- 1 to account for the commas 'do your sql stuff here, s1 will hold the parsed value ' loop |
|
#3
|
|||
|
|||
|
Hi Doug,
umm, I'm a little lost here... I couldn't get the loop to run. the n2 value is equal to 0. Last edited by kellylei : August 20th, 2003 at 11:46 AM. |
|
#4
|
|||
|
|||
|
Why not use the IN keyword?
Just a thought! <% Dim str Dim strSql str = "2,3,4,5" strSql = "SELECT * " & _ "FROM Table1 " & _ "WHERE code IN (" & str & ") 'FOR DEBUG ONLY 'Response.Write strSql & "<hr>" 'Response.End %> Hope this helps! Sincerely Vlince |
|
#5
|
|||
|
|||
|
Hi Vlince,
Thanks but I need to know exactly which code being selected. I need to compare the code in two tables. If code 2 exist in Table1, I will not insert anything into Table1. If code 2 does not exist I will execute the insert statement. Thanks for sharing your thoughts! Let me know if you have better idea. |
|
#6
|
|||
|
|||
|
AHHHHHHHHHHH...............ok
You know Kelly these informations would've been nice to know in your *first* post... In that case, look at what's inside the attachment file... Hope this helps! Sincerely Vlince |
|
#7
|
|||
|
|||
Sorry, I tried to simplify my question so it won't be too difficult to understand.I have tried the attached coding, but I got this error message. Object required: '' for line ---> Set objRst = objConn.Execute(strSql) Is there something I should change to test this script? |
|
#8
|
|||
|
|||
|
Well the attachment file was to give you an idea of what to do NOT the exact working thing!
Did you create a connection object ? In the example, I called it objConn I even took the time to say/write: '----------------------------------------------------- 'Open the connection object BEFORE entering the LOOP '----------------------------------------------------- So did you? I mean create the connection object? Vlince |
|
#9
|
|||
|
|||
|
It works now!
I accidentally moved the line set rs = Server.CreateObject("ADODB.Recordset") before the for loop. Thank you so much! XOXO for you I have never thought about the Split function. Thanks again. Last edited by kellylei : August 20th, 2003 at 02:46 PM. |
|
#10
|
|||
|
|||
|
Wait a minute...
I never showed/told you to EXPLICITly declare a recordset object? You're creating too much overhead inside the Loop each time you do: Set rs = Server.CreateObject("ADODB.Recordset") That's why I simply created an IMPLICIT recordset to avoid the overhead! Humm...but if it works for you then I'm happy! Oh and...can I share them(XOXO) with others? Hope this helps! Sincerely Vlince Last edited by Vlince : August 20th, 2003 at 02:48 PM. |
|
#11
|
|||
|
|||
|
Dang, Vlince stole my thunder. "Split" is one of the most useful (and easy to use as well) string functions in the whole VB library.
Beats the socks off of INSTR in most cases. |
|
#12
|
|||
|
|||
|
man, foreign guys get ALL the chicks, how can i compete? It's impossible...
|
|
#13
|
|||
|
|||
|
ROTFLMAO...
Was drinking water and choked...got water coming out of my nose thanks to you...It hurts LOL foreign guys! LOL good one ![]() It's my "little more white skin color" from all the snow that works like a charm on girls...and my french of course! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ASP Programming > String with commas, how to use instr and mid function in a loop? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|