Wrox Programmer Forums
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 3rd, 2006, 12:53 PM
Authorized User
Join Date: Jun 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default ASP dynamic SQL query with for loop?


I am trying to build a dynamic SQL query in asp using a "for loop".
I can get the "for loop" results to display fine in browser using "response.write"
BUT I want to put the "for loop" results into an SQL query.

Here is the code which genrates syntax errors:

Recordset2.Source = "SELECT * FROM Products WHERE " & _
For fnum2 = 4 To Recordset1.Fields.Count-1
    If Recordset1.Fields(fnum2).Value = 1 then
       Product_Code = '" & _Recordset1.Fields(fnum2).Name & "' AND"
    End if

Please help!!
Old April 4th, 2006, 12:37 PM
Authorized User
Join Date: Jun 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts

Nobody on this forum provided any help :(

Anyway a friend managed to help me and solved it.
Old April 4th, 2006, 01:19 PM
Friend of Wrox
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts

Can you do a Response.Write on the page so I can see what query got built?

One thing that I do when building dynamic queries is to append the following code at the end of the SQL statement since you never know how many "ANDs" you will need.

 Product_Code = Product_Code & " 1 = 1 ;"

Its hard to tell from what you posted exactly what the problem is.


Old December 13th, 2006, 08:56 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts

I think for starters the product code needs to be in the string.
  Product_Code = '" & _Recordset1.Fields(fnum2).Name & "' AND" <--the product code does not appear to be part of the string.
  "Product_Code = '" & _Recordset1.Fields(fnum2).Name & "' AND"
Not tested could be wrong.
But rstelma is correct, print the sql query with
response.write(Recordset2.Source )

They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Table Name in SQL Query drani C# 2 November 6th, 2007 05:47 PM
Dynamic SQL Query Problem HydroChronic Access VBA 10 October 4th, 2007 09:27 AM
dynamic session variable in while loop ashuphp Beginning PHP 0 May 3rd, 2007 04:59 AM
Sql Query Help - ASP cancer10 Classic ASP Databases 8 November 7th, 2006 03:19 PM
Sql Query in ASP mateenmohd SQL Server 2000 3 October 29th, 2003 12:16 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.