Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll

my Blog
[prev. Lesson]  SQL: Search Forms #3
     [next Lesson]  SQL: Search AND/OR Examples

SQL OR Search Example by Charles Carroll

AND and OR operators expand the power of the WHERE clause and provide a powerful tool to check multiple conditions. The Basic Guidelines are as follows:  

If your goal is that several if conditions must ALL BE TRUE to suceed, this is the Role of the AND within a WHERE clause, i.e.
"select * from publishers where state='MD' and city='Rockville'
"select * from authors where Year_Born>1960 and Year_Born<1970'

If several conditions can indivually be true this is the Role of an OR within a WHERE clause, i.e.
"select * from publishers where state='MD' OR state='NY'

   filename=/learn/test/SQLcities.asp

<Test Script Below>


<HEAD><TITLE>SQLcities.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<Form action = "SQLcitiesrespond.asp" method="POST">
Choose City (or Cities):<p>
<% 
call query2listm("select distinct city from publishers", _
   "cy","DSN=student;uid=student;pwd=magic")
%>
<P>
<Input type="submit" value="Get Data">&nbsp;<Input type="reset" value="Clear City"></form>
</BODY></HTML>
<!--#include virtual="/learn/test/lib_dblistm.asp"-->

The responder looks like this:

   filename=/learn/test/SQLcitiesrespond.asp

<Test Script Below>


<HEAD><TITLE>sqlcitiesrespond.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
citycount=request.form("cy").count
If citycount=0 then%>
    <B>You never choose a city!</b><br>
    <a href="sqlcities.asp">Choose City</a>
<%
    response.end
end if
firstcity=request.form("cy")(1)
SQLtemp="select * from publishers "
SQLtemp = SQLtemp & " where city='" & firstcity & "'" 
for counter=2 to citycount
    whichcity=request.form("cy")(counter)
    SQLtemp = SQLtemp & " or city='" & whichcity & "' "
next
response.write SQLtemp
call query2table(SQLtemp,"DSN=student;uid=student;pwd=magic")
%>
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
</BODY></HTML>

The file lib_dbtable.asp looks like this:

   filename=/learn/test/lib_dbtable.asp

<Test Script Below>


<% 
sub query2table(inputquery, inputDSN)
    dim conntemp, rstemp
    set conntemp=server.createobject("adodb.connection")
    conntemp.open inputDSN
    set rstemp=conntemp.execute(inputquery)
    howmanyfields=rstemp.fields.count -1%>
    <table border=1><tr>
    <% 'Put Headings On The Table of Field Names
    for i=0 to howmanyfields %>
            <td><b><%=rstemp(i).name%></B></TD>
    <% next %>
    </tr>
    <% ' Now lets grab all the records
    do while not rstemp.eof %>
        <tr>
        <% for i = 0 to howmanyfields
            thisvalue=rstemp(i)
            If isnull(thisvalue) then
                thisvalue="&nbsp;"
            end if%>
            <td valign=top><%=thisvalue%></td>
        <% next %>
        </tr>
        <%rstemp.movenext
    loop%>
    </table>
    <%
    rstemp.close
    set rstemp=nothing
    conntemp.close
    set conntemp=nothing
end sub%>

The file lib_dblistm.asp looks like this:

   filename=/learn/test/lib_dblistm.asp

<Test Script Below>


<%
SUB query2listm(myquery,myname,myDSN)
    dim conntemp, rstemp
    set conntemp=server.createobject("adodb.connection")
    conntemp.open myDSN
    set rstemp=conntemp.execute(myquery)
    %>
    <Select name="<%=myname%>" multiple>
    <%
    do while not rstemp.eof
        thisfield=trim(RStemp(0))
        if isnull(thisfield) or thisfield="" then
            ' ignore
        else
            response.write "<option>" & thisfield & "</option>"
        end if
        rstemp.movenext
    loop
        %>
    </select>
    <%rstemp.close
    set rstemp=nothing
    conntemp.close
    set conntemp=nothing
END SUB
%>

Chaz Wish List
Tall Tip $5
Grande Tip $20
Venti Tip $39
Tip Jar Thanks
2004 Thanks
2005 Thanks
HUGE Tip -love site