Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to nest loop in sql statement

Status
Not open for further replies.

frogggg

Programmer
Jan 17, 2002
182
US
I have a sql statement which brings up all records which relate to criteria set on a page before in session variables. One of the criteria needs to be multiple select, so I have the following array to get all the values from the listbox and put them in a session variable:

'allow for multiple select
dim intCount
'dynamic array
dim arraySkills()

'for each selected element in listbox
for intCount=0 to Request.Form.Item("lstSkills").count - 1
'dynamically increase array size but preserve contents
redim preserve arraySkills(intCount)
'populate array
arraySkills(intCount) = Request.Form.Item("lstSkills")(intCount + 1)
next

'put array in session variable
session("SearchSkills") = arraySkills

This works fine.

The question is how to use this info in a where clause of my sql statement. Usually I would retrieve the contents of the array, element by element, like this:

for intCount = 0 to ubound(session.contents("SearchSkills"))
response.write session.contents("SearchSkills")(intCount)
next

How on earth would I get this information in a sql statement?

Any help would be greatly appreciated!
 
You can use the IN keyword. The SQL Statement would look like this example from SQL Server. Notice the example uses a char type field since it has quotes around the value. It might be better to build a comma delimited string instead since that would more closely match the IN list.

SELECT pub_name
FROM publishers
WHERE pub_id in ('1389', '0736')

Put something like this in the loop but remove the comma on the last occurs.
Dim str
str = str & Request.Form.Item("lstSkills")(intCount + 1) & ", "




 
I'm not sure I understand - what does the keyword in mean, and what are the values you have of in? Can I put the array name right there, or do I have to fish out the values and put them in a variable one by one?

Thanks for your help.
 
The SQL keyword IN, says to retrieve the records that match the IN list. You would need to take the values out of the array as it is a comma delimited list, that is why it is probably easier to build a comma delimited list in your loop where you are now filling the array. This way all the values are in a variable that can be used when building the SQL string.
 
Thanks!
Now I have the strangest problem:
I'm getting Subscript out of range: 'ubound' error
This is the identical code for many other listboxes that I have on my pages. Here is my code - do you see the problem?

dim intCount
'dynamic array
dim arraySkills()

'for each selected element in listbox
for intCount=0 to Request.Form.Item("lstSkills").count - 1
'dynamically increase array size but preserve contents
redim preserve arraySkills(intCount)
'populate array
arraySkills(intCount) = Request.Form.Item("lstSkills")(intCount + 1)
next

'put array in session variable
session.Contents("SearchSkills") = arraySkills

That was on the first page. This is on the second page to get the values:
dim intCount
dim skills

for intCount = 0 to ubound(session.Contents("SearchSkills"))

skills = skills & session.Contents("SearchSkills")(intCount) & ","
next


Also, how do I get rid of the comma on the last run of the loop?

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top