Adding a variable in a SQL statement???

Results 1 to 2 of 2

Thread: Adding a variable in a SQL statement???

  1. #1
    Join Date
    Dec 1969

    Default Adding a variable in a SQL statement???

    HI! Hope someone can help me...<BR><BR>I&#039;m trying to use a variable in my ASP SQL with no luck...<BR><BR>strSQL = "SELECT * FROM tablename WHERE "... (tablename is actual name of table in .mdb...) works fine...<BR><BR>But if I try to use a declared variable set as "tablename" i.e:<BR><BR>CHOICE = "tablename" then<BR><BR>strSQL = "SELECT * FROM CHOICE WHERE "... <BR><BR>I get an error &#039;cannot find the table...&#039;<BR><BR>The variable works, I&#039;ve tested it by response.writing it to a test page. Is there some syntax trick about dropping a variable into SQL that I don&#039;t know about??<BR><BR>Any ideas would be greatly appreciated!Thanx!<BR><BR>mobius<BR><BR>

  2. #2
    Join Date
    Dec 1969

    Default *ANYTHING* inside a string... by DEFINITION *not* REPEAT NOT a variable!<BR><BR>If you put "..." around something, that&#039;s what you get. Period. Verbatim. Unchanged.<BR><BR>So when you do<BR> strSQL = "SELECT * FROM CHOICE WHERE "... <BR><BR>Then by golly, you sure as heck better have a table named CHOICE in your DB, or you ain&#039;t gonna get *NUTTIN*.<BR><BR>Understand that the *ONLY* thing that the SQL processor sees is the *FINAL FORM* of your string.<BR><BR>In other words, if you do<BR> Response.Write "DEBUG: strSQL is " & strSQL & "&#060;HR&#062;"<BR>just before you send that strSQL off to the DB, then whatever that shows is *exactly* what the SQL processor is going to have to work with.<BR><BR>Now *surely* by now, you&#039;ve learned how to build the WHERE clause using form input, etc. No?<BR><BR>strSQL = "SELECT * FROM tablename WHERE id = " & Request("userID")<BR><BR>or something along those lines? <BR><BR>So why would you think that building a string to get the table name would be different?<BR><BR>strSQL = "SELECT * FROM " & CHOICE & " WHERE id = " & Request("userID")<BR><BR>Understand?<BR><BR><BR><B R>

Posting Permissions

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