473,473 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Invalid SQL - Please help .........

Hi,

I cannot get the following (MS Access) SQL statement working in my asp
page, please can anyone help me ? Thanks :-)

------------------------------------------------
<%

strQuery = "SELECT Customers.CustomerName, OrderLines.JobNumber,
Orders.PONumber, OrderLines.OrderQuantity,
Sum(StockMovements.QtyShipped) AS SumOfQtyShipped,
[OrderQuantity]-Sum([QtyShipped]) AS RemainingUnits,
OrderLines.OrderNotes, Products.ProductRangeID, OrderLines.SelectAll,
PCBForecast.ShipQty, Products.ProdCode, PCBForecast.HeldMarker,
Products.ProductID, PCBForecast.ShipETA"
strQuery = strQuery & " FROM (Customers INNER JOIN (Orders INNER JOIN
(Products INNER JOIN (OrderLines INNER JOIN StockMovements ON
OrderLines.JobNumber = StockMovements.JobNumber) ON (Products.ProductID
= OrderLines.ProductID) AND (Products.ProductID =
OrderLines.ProductID)) ON Orders.OrderID = OrderLines.OrderID) ON
Customers.CustomerID = Orders.CustomerID) INNER JOIN PCBForecast ON
OrderLines.JobNumber = PCBForecast.JobNumber"
strQuery = strQuery & " GROUP BY Customers.CustomerName,
OrderLines.JobNumber, Orders.PONumber, OrderLines.OrderQuantity,
OrderLines.OrderNotes, Products.ProductRangeID, OrderLines.SelectAll,
PCBForecast.ShipQty, Products.ProdCode, PCBForecast.HeldMarker,
Products.ProductID, PCBForecast.ShipETA"
strQuery = strQuery & " HAVING
((([OrderQuantity]-Sum([QtyShipped]))>=0) AND
((OrderLines.SelectAll)=Yes) AND ((PCBForecast.HeldMarker)="Scheduled")
AND ((PCBForecast.ShipETA) Between Now() And #12/30/3000#));"

Set RS = adoDataConn.Execute(strQuery)
if RS.EOF then
response.write "Boo Hoo"
else
response.write "Hooray"
end if
%>

----------------------------------------------------------------------------

Thank you very much :-)

Sep 25 '06 #1
1 2304
David wrote:
Hi,

I cannot get the following (MS Access) SQL statement working in my asp
page, please can anyone help me ? Thanks :-)
Please describe your symptoms without using the words "not working" or
"can't get it to work".
Is the subject of this post related to an error message you are receiving?
If so, the only way to debug a sql statement is to look at it. All you have
shown us in the vbscript code that is supposed to result in a sql statement.
You need to look at the generated statement itself. The only way to do that
is:

Response.Write strQuery
Response.End

Run the page and look at the result. is it what you expect it to be? If
you've done it correctly, you should be able to copy the statement from the
browser window to the clipboard, open your database in Access, create a new
query in Design View, switching to SQL View at the first opportunity, paste
the statement into the sql window and run it it without modification (with a
couple exceptions that don't apply here). Usually, Access will give you a
better error message than the one supplied to vbscript by ADO.

Now, I have looked at the vbscript, and I do see a problem with it here:

.... ((PCBForecast.HeldMarker)="Scheduled") ...

What do yyou expect the vbscript compiler to do when it encounters that
double quote preceding Sch in the string? Well, the compiler cannot read
your mind. All it can see is that you used a double quote to delimit the
beginning of the string, and therefore, when it encounters a second double
quote, it is expecting to end the string expression. But no, more characters
appear after the quote so the compiler raises an error because it does not
know how to deal with them.

There are two ways to handle this:
1. "Escape" the double quote, so the compiler treats it as a literal quote
in the string rather than a delimiter. In vbscript (and sql for that matter)
characters are escaped by doubling them. When the compile encounters two
double quotes, it realizes that you want it to insert a literal value into
the resulting string:
.... ((PCBForecast.HeldMarker)=""Scheduled"") ...

2. SQL allows the use of single quotes (apostrophes) to delimit literal
string values in sql statements. So the above can be changed to:
.... ((PCBForecast.HeldMarker)='Scheduled') ...
Of course, this presents its own special problems, when the data being
supplied contains apostrophes (O'Malley). So, the apostrophes in the data
being supplied now have to be escaped.
Now let me address the efficiency of this query you are creating: you have
put all the filtering criteria in the HAVING clause. This is not a good
idea. The only filtering criteria that should appear in the HAVING clause
are those that concern aggregated columns (columns resulting from aggregate
functions like SUM, COUNT, etc.) in the grouping query. All other criteria
should be put in a WHERE clause that appears BEFORE the GROUP BY clause.
This is so the records can be filtered BEFORE the grouping is applied.
Maximizing a grouping queries performance involves minimizing the number of
records that have to be grouped. So, your query should look like this:

SELECT ...
FROM ...
WHERE OrderLines.SelectAll)=Yes AND PCBForecast.HeldMarker ='Scheduled' AND
PCBForecast.ShipETA Between Now() And #12/30/3000#
GROUP BY ...
HAVING [OrderQuantity]-Sum([QtyShipped])>=0
BTW, why not simplify this to:
.... PCBForecast.ShipETA >= Now() ...
Lastly, especially with a huge query statement like this, I would not build
it in vbscript. i would create a saved query in Access and execute the saved
query via ADO. See:
http://groups.google.com/group/micro...d322b882a604bd

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Sep 25 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anup Jishnu | last post by:
Hi, I have installed ASP.Net application on a system. When accessing the Application from within the LAN, it works fine. However, when I access the application from the Internet, some pages...
8
by: Glenn A. Harlan | last post by:
Why am I receiving the below error when calling - Path.GetTempFileName() The directory name is invalid. Description: An unhandled exception occurred during the execution of the current web...
30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
2
by: Brad | last post by:
I have an intranet app that has just started sporadically getting the following error "The viewstate is invalid for this page and might be corrupted." By sproadic I mean 3-4 times during the past...
5
by: Anup Jishnu | last post by:
Hi, I have installed ASP.Net application on a system. When accessing the Application from within the LAN, it works fine. However, when I access the application from the Internet, some pages...
1
by: King Kong | last post by:
we are facing this kind of error when we double click the infragistic web grid please help me on this Regards Moid Iqbal Server Error in '/NetworkAccess' Application....
9
by: MR | last post by:
I get the following Exception "The data at the root level is invalid. Line 1, position 642" whenever I try to deserialize an incoming SOAP message. The incoming message is formed well and its...
2
by: none | last post by:
Hi, I'm opening a popup window with JavaScript. It has a command button. This is from the primary source (opener) window: ###################################################################...
1
by: Brett | last post by:
I have a DropDownList in an ASP.NET web form that is populated with items from a lookup table by binding that DropDownList to a SqlDataSource. However, the items in the lookup table can change over...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.