Solved

SQL recordset syntax problem

Posted on 2004-04-26
6
526 Views
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.Open()

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.Open()

rsPages_numRows = 0
%>
<html>
<head>
<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>
</head>

<body>
<p>
  <script id="Sothink Widgets:dynmenutest.pgt" type="text/javascript" language="JavaScript1.2">
<!--
stm_bm(["menu1b7a",400,"","blank.gif",0,"","",0,0,250,0,1000,1,0,0,"","",0],this);
stm_bp("p0",[1,4,0,0,2,3,0,7,100,"",-2,"",-2,50,0,0,"#fffff7","#fffff7","",3,1,1,"#000000"]);
<%
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]);
stm_bpx("p1","p0",[1,2,0,0,2,3,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]);
<%
rsPages.MoveNext
Wend
%>

stm_ep();
<%rsSections.MoveNext%>
<%Wend%>
stm_ep();
stm_em();
//-->
</script>


0
Comment
Question by:matsos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:dakyd
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)'"
to:
rsPages.Source = "SELECT * FROM tblPages WHERE SectionID = " & rsSections(0)

Hope that helps.
0
 

Author Comment

by:matsos
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
0
 
LVL 7

Accepted Solution

by:
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).
0
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
LVL 19

Expert Comment

by:dakyd
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.
0
 

Author Comment

by:matsos
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.
0
 

Author Comment

by:matsos
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.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

622 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