Putting SQL Statements Together

Results 1 to 2 of 2

Thread: Putting SQL Statements Together

  1. #1
    Join Date
    Dec 1969
    Posts
    1,671

    Default Putting SQL Statements Together

    I&#039ve got a beginner&#039s question too:<BR><BR>I have a form where the user can search a database using name, e-mail, and some other fields. Only the name field is required for a search, and the other fields can be specified as well if the user wants. My question is: Is there an easy way to create the SQL statement without really knowing which fields they will be searching by? Obviously the statement can&#039t be hardcoded in, but even if I check to see which fields the user has specified, I still have to come up with a way of getting the WHERE&#039s etc. to fit together?<BR><BR>Thanks in advance,<BR>Mike

  2. #2
    Join Date
    Dec 1969
    Posts
    1,019

    Default RE: Putting SQL Statements Together

    sqltxt="SELECT * FROM tblName WHERE "<BR>if request("formfield1")&#060;>"" then<BR>&#039if the info passed is not empty, add the query text<BR> sqltxt=sqltxt & " (field1=&#039" & request("formfield1") & "&#039) AND "<BR>end if<BR>if request.form("formfield2")&#060;>"" then<BR> sqltxt=sqltxt & " (field2=" & request("formfield2") & ") AND "<BR>end if<BR>&#039you get the idea. Of course, since you&#039ve stated WHERE you&#039ll &#039need to enter a bogus search that will never be true in the &#039event the user enters nothing<BR>sqltxt=sqltxt & " (favoritecolor=&#039ldldldldldld&#039) "<BR>dbconn.execute(sqltxt)<BR>&#039You can also reponse.write the sqltxt while testing to see the &#039exact query<BR>

Posting Permissions

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