wrong variable syntax in querys

Results 1 to 2 of 2

Thread: wrong variable syntax in querys

  1. #1
    Join Date
    Dec 1969
    Posts
    17

    Default wrong variable syntax in querys

    I have a question about the syntax of using variables in querys. <BR><BR>here is the code that I have come up with so far:<BR><BR>The static version of what i am doing now works, but doesn&#039t use a variable:<BR><BR>&#060;%<BR>&#039Create a query sysprocesses<BR>rs.Open "SELECT Name FROM newpages WHERE Name LIKE &#039a%&#039", conn<BR>%&#062;<BR><BR>i would like to replace the "a" with a variable so that i can have a loop and go through all the letters of the alphabet. but i keep having syntax errors. this is how far i am...<BR><BR>&#060;%<BR>variablename = "a"<BR>&#039Create a query sysprocesses<BR>rs.Open "SELECT Name FROM newpages WHERE Name LIKE &#039" & &#060;%=variablename%&#062; & "%&#039", conn<BR>%&#062;<BR><BR>right now i am just setting variablename to "a" until the query works and i get the syntax correct.<BR><BR>Any help, hummor, or hints would be greatly appreciated<BR><BR>-Timm

  2. #2
    Join Date
    Dec 1969
    Posts
    2,849

    Default RE: wrong variable syntax in querys

    You tried:<BR>rs.Open "SELECT Name FROM newpages WHERE Name LIKE &#039" & &#060;%=variablename%&#062; & "%&#039", conn<BR><BR>and it didn&#039t work. That&#039s because of the &#060;%=...%&#062;. You only use that when you are NOT in an ASP script block. See, the &#060;%...%&#062; are delimiters telling IIS that we are inside of an ASP block (i.e. code in here needs to be processed before sent to the browser).<BR><BR>So, doing:<BR><BR>&#060;%<BR>&#039stuff<BR><BR>&#060;% &#039more stuff %&#062;<BR>%&#062;<BR><BR>doesn&#039t make sense. You can&#039t have ASP script blocks within other ASP script blocks. So, get rid of those &#060;%=...%&#062; and do:<BR><BR>rs.Open "SELECT Name FROM newpages WHERE Name LIKE &#039" & variablename & "%&#039", conn<BR><BR>That will do the trick for you! :)<BR>Also, I highly recommend that you do:<BR><BR>Dim strSQL<BR>strSQL = "SELECT Name FROM newpages WHERE Name LIKE &#039" & variablename & "%&#039"<BR>objRS.Open strSQL, conn<BR><BR>This way, when you&#039re testing and having problems (i.e. database errors or you are just not getting the correct results back), you can put:<BR><BR>Response.Write "SQL = " & strSQL<BR><BR>right before your objRS.Open. This will let you see exactly what SQL is getting passed.<BR><BR>Happy Programming!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •