Re: Need some help with simple search query

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Drew (dlaing_at_NOswvtc.state.va.SPAMus)
Date: 07/14/04


Date: Wed, 14 Jul 2004 12:18:27 -0400

Please forgive my ignorance, but that is Greek to me... I am using this for
an ASP page, where do I keep this? Do I create a stored procedure?

Thanks,
Drew

"kirk1880" <kirk1880@discussions.microsoft.com> wrote in message
news:93B8A3DA-FCB2-4CA8-BEE9-7A2CA78B229E@microsoft.com...
> 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: webbased search through catalog
    ... I've got the asp script working, but it seems to spit out results ... Modify q.catalog to specify your catalog name. ... > ' your logo is not a GIF file, or you don't want to copy it, change the ... > ' You can set the number of query results returned on a single page ...
    (microsoft.public.inetserver.indexserver)
  • Re: Complex query criteria - desperate appeal
    ... I made an ASP website using an Access DB ... criteria listboxes have the ALL choice, and the value passed to the query is ... I then build a string of the selected ID's and use ...
    (microsoft.public.access.queries)
  • Re: Like Operator with Unicode
    ... why don't you set up the database to use a Greek collation instead? ... Sylvain Lafontaine, ing. ... are setting as the sql string in your querydef should be passed to ... Notice that I have replace the character * with % because this query will ...
    (microsoft.public.access.queries)
  • Re: ASP.NET 2.0 Easier than ASP? Gimmie a Break!
    ... grid/table with the results from a query. ... I love asp.net, the possibilities compared to imo flawed asp is much ... database, display a grid/table, and not have much to worry about. ... I've seen a few online examples writing an n-tier design for ASP.NET 2.0 ...
    (microsoft.public.dotnet.framework.aspnet)
  • search query help
    ... I had an ASP intranet app up for a while, in use by many at my company. ... query which will search two targeted varchar fields and: ... So if a user wanted to search on "database error 30008", ... SQL Server database, because that's where the data is kept. ...
    (microsoft.public.sqlserver.programming)