Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP 3 Classic ASP Active Server Pages 3.0 > Classic ASP Databases
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 3rd, 2006, 12:53 PM
Authorized User
 
Join Date: Jun 2004
Location: , , .
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default ASP dynamic SQL query with for loop?

Hi,

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:

Code:
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
Next

Please help!!
Reply With Quote
  #2 (permalink)  
Old April 4th, 2006, 12:37 PM
Authorized User
 
Join Date: Jun 2004
Location: , , .
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nobody on this forum provided any help :(

Anyway a friend managed to help me and solved it.
Reply With Quote
  #3 (permalink)  
Old April 4th, 2006, 01:19 PM
Friend of Wrox
Points: 2,101, Level: 18
Points: 2,101, Level: 18 Points: 2,101, Level: 18 Points: 2,101, Level: 18
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2003
Location: , , .
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

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.

Thanks,
Richard

Reply With Quote
  #4 (permalink)  
Old December 13th, 2006, 08:56 PM
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

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.
======================================
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:48 AM.


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