RE: Need some help with simple search query

From: kirk1880 (kirk1880_at_discussions.microsoft.com)
Date: 07/14/04


Date: Wed, 14 Jul 2004 09:06:04 -0700

There are two ways you can do this:

1st and the perfered way is to write out all the tedious logic

IF @v_variable1 IS NOT NULL
BEGIN -- 1 is not null
  
  IF @v_variable2 IS NOT NULL
  BEGIN -- 2 is not null

    IF @v_variable3 IS NOT NULL
    BEGIN -- 3 is not null
      SELECT whatever
        FROM whatever
      WHERE column1 = @v_variable1
          AND column2 = @v_variable2
          AND column3 = @v_variable3

    END -- 3 is not null
    ELSE
    BEGIN -- 3 is null

      SELECT whatever
        FROM whatever
      WHERE column1 = @v_variable1
          AND column2 = @v_variable2

    END -- 3 is null

  END -- 2 is not null
  ELSE
  BEGIN -- 2 is null

      SELECT whatever
        FROM whatever
      WHERE column1 = @v_variable1

  END -- 2 is null

END -- 1 is not null

IF @v_variable1 IS NULL AND @v_variable2 IS NOT NULL
BEGIN
  and so forth and so forth.....

2nd way (and I would only recomend this if the query is going against a small result set)

DECLARE @v_select NVARCHAR(2000),
              @v_where NVARCHAR(2000)

SELECT @v_select =
            'SELECT Whatever ' + CHAR(13) +
            ' FROM whatever',
            @v_where = ''

IF @v_variable1 IS NOT NULL
BEGIN
  SELECT @v_where = @v_where + 'AND col1 = @v_variable1' + CHAR(13) + ' '
END

IF @v_variable2 IS NOT NULL
BEGIN
  SELECT @v_where = @v_where + 'AND col1 = @v_variable2' + CHAR(13) + ' '
END

and so forth and so forth until you get through all the variables, then

SELECT @v_select = @v_select + STUFF(@v_where, 1, 3, 'WHERE')

EXEC dbo.sp_executesql @v_select

"Drew" wrote:

> I have the following query,
>
> SELECT CA.RegNo, CA.ApptDoctor, CA.ApptDate, CA.ApptTime, CA.ApptNotes,
> AC.ApptCatName, C.CliLName, C.CliMM, C.CliFName
> FROM CliDrAppts.dbo.tblCliAppts CA INNER JOIN CliCore.dbo.tblClients C ON
> CA.RegNo = C.RegNo INNER JOIN tblCliApptCat AC ON CA.ApptCatID =
> AC.ApptCatID
>
> This joins all the tables I need and now I need to get some WHERE clauses in
> there. I have a page that has 4 form elements on it, a dropdown with all
> the months in it, a dropdown with years in it (from 2004 to 2020), a dynamic
> dropdown with Resident's names and a dynamic dropdown with Appointment
> Categories. I am having problems with this because if you omit one of these
> search criteria, then it doesn't return any results. Here is my new query,
>
> SELECT CA.RegNo, CA.ApptDoctor, CA.ApptDate, CA.ApptTime, CA.ApptNotes,
> AC.ApptCatName, C.CliLName, C.CliMM, C.CliFName
> FROM CliDrAppts.dbo.tblCliAppts CA INNER JOIN CliCore.dbo.tblClients C ON
> CA.RegNo = C.RegNo INNER JOIN tblCliApptCat AC ON CA.ApptCatID =
> AC.ApptCatID
> WHERE LEFT(ApptDate,2) = var1 AND RIGHT(ApptDate,4) = var2 AND CA.ApptCatID
> = var3 AND CA.RegNo = var4
>
> var1 = request.form("Month")
> var2= request.form("Year")
> var3 = request.form("Category")
> var4 = request.form("Resident")
>
> How can I make this so that if the user wants to search for records
> pertaining to just the Resident, or if the user wants to search for records
> in a specific month. I thought about using something like this,
>
> If request.form("Month") <> "" Then
> sql = blah,blah
> Else
> sql = blah,blah
>
> But there are too many combinations, I just think there should be an easier
> way to do this, but the OR in the SQL Statement doesn't work either, then it
> returns too many records.
>
> Thanks,
> Drew
>
>
>



Relevant Pages

  • Re: Quick question about search fields+drop-down menus...
    ... try putting parentheses around your search criteria; ... >> copy and paste something similar into the custom query window of the>> DRW): ... > The original posted query was bugged for some reason and my knowledge> of SQL ...
    (microsoft.public.frontpage.client)
  • ropdownlist listitem.value not working - returns listitem.text instead
    ... I am populating a dropdownlist from SQL. ... after item #5 in the dropdown box. ... If I run the query in Query Analyzer, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: sorting query by calculated field
    ... No. Open the Query in design view. ... design toolbar as a dropdown to select SQL view, ... the View menu option to view the query in its *real* form - the SQL ...
    (microsoft.public.access.queries)
  • Re: criteria
    ... When go into SQL Specific there is Union, Pass through, Data Definition: ... Also do I start with the word parameter. ... or selecting the SQL button from the dropdown ... table window in query design view and select Paramteters from the menu. ...
    (microsoft.public.access.queries)
  • Re: Date limited query?????
    ... Sara, if you open the query in desgin mode you should see an icon just below ... on the dropdown and select SQL View, that is what you need to paste here so ...
    (microsoft.public.access.forms)