Solved

SQL recordset syntax problem

Posted on 2004-04-26
6
461 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
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:dakyd
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 19

Expert Comment

by:dakyd
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now