SQL recordset syntax problem

Posted on 2004-04-26
Last Modified: 2012-05-04
Hello guys,

I am trying to setup a dynamic menu that gets columns from the db "sections" table and rows from the "pages" table.

I get the following Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[MySQL][ODBC 3.51 Driver][mysqld-4.0.18-nt]You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ''SELECT * FROM tblPages WHERE SectionID=' & 'rsSections(0)'' at dynmenutest.asp, line 30

There seems to be a problem on the SQL syntax of the rsPages recordset.

Here's the code of my page:

Dim Dbq, Dsn, Dc
Dim sSectionName, sPage, sLink
Dim rsSections
Dim rsSections_numRows

Set rsSections = Server.CreateObject("ADODB.Recordset")
rsSections.ActiveConnection = MM_City_conn_STRING
rsSections.Source = "SELECT * FROM city.sections"
rsSections.CursorType = 0
rsSections.CursorLocation = 2
rsSections.LockType = 1

rsSections_numRows = 0
Dim rsPages
Dim rsPages_numRows

Set rsPages = Server.CreateObject("ADODB.Recordset")
rsPages.ActiveConnection = MM_City_conn_STRING
rsPages.Source = "'SELECT * FROM tblPages WHERE SectionID=' & 'rsSections(0)'"
rsPages.CursorType = 0
rsPages.CursorLocation = 2
rsPages.LockType = 1

rsPages_numRows = 0
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" language="JavaScript1.2" src="stm31.js"></script>

  <script id="Sothink Widgets:dynmenutest.pgt" type="text/javascript" language="JavaScript1.2">
While Not rsSections.EOF
sSectionName = rsSections("Section")

stm_ai("p0i0",[1,"<%=sSectionName%>","","",-1,-1,0,"","_self","","","","",0,0,0,"arrow_r.gif","arrow_r.gif",7,7,0,0,1,"#fffff7",0,"#b5bed6",0,"","",3,3,1,1,"#fffff7","#000000","#000000","#000000","8pt Verdana","8pt Verdana",0,0]);
While Not rsPages.EOF
sPage = rsPages("PageMenuName")
sLink = rsPages("pn")

stm_ai("p1i0",[1,"<%=sPage%>","","",-1,-1,0,"<%=sLink%>","_self","","","","",0,0,0,"","",0,0,0,0,1,"#fffff7",0,"#b5bed6",0,"","",3,3,1,1,"#fffff7","#000000","#000000","#000000","8pt Verdana","8pt Verdana",0,0]);


Question by:matsos
  • 3
  • 2
LVL 19

Expert Comment

ID: 10920703
it looks like you're not concatenating the strings properly for your query

change this line:
rsPages.Source = "'SELECT * FROM tblPages WHERE SectionID=' & 'rsSections(0)'"
rsPages.Source = "SELECT * FROM tblPages WHERE SectionID = " & rsSections(0)

Hope that helps.

Author Comment

ID: 10920810
Thanks dakyd for your comment.

Although it seems that your syntax is correct, it returned the same error again with one difference. It returned the first value of the recordset.

Here it is:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[MySQL][ODBC 3.51 Driver][mysqld-4.0.18-nt]You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'City College' at line 1

Accepted Solution

minnirok earned 500 total points
ID: 10921455
You need to enclose the field value in single quotes for the SQL query.  So change it to:

rsPages.Source = "SELECT * FROM tblPages WHERE SectionID = '" & rsSections(0) & "';"

should work.  Note that there are single quotes (') now within your query.  Without these, SQL won't understand that 'City College' is a value (rather than a field or command).
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

LVL 19

Expert Comment

ID: 10921661
minnirok is right, any strings need to wrapped with single quotes or SQL won't realize they are values.  I had assumed that SectionID was an integer, so I left them off.  Hopefully you have what you want now.

Author Comment

ID: 10921737
Thanks for all your replies guys. The page worked fine, no errors, but there still seems to be a problem as the recordset rsPages did not return any values. :-(

Section_ID is an integer though.

Author Comment

ID: 10921881
Ok I figured out what the problem was. The field of the Sections table was not the first field to be retrieved in the Mysql db.
I wish I could give the points to both of you.
Thanks for your help.

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Read about achieving the basic levels of HRIS security in the workplace.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question