Home All Groups Group Topic Archive Search About
Author
2 Jul 2005 4:12 AM
Stuart Hawkins
Hi,
I have a Select query I have put in a stored procedure.  What I would really
like to do is pass it parameters that tell it not only the values to filter
by but the fields to filter on too (choice of three including combination of
more than one).

Cannot figure it out!  Any help welcome.

SP eventually called by VB6 program
Have already sucesfully passed it date paramaeters

Thanks!

Author
2 Jul 2005 4:19 AM
oj
You're looking for dynamic query.

I suggest you take a look at Erland's article for pro/cons.

http://www.sommarskog.se/dyn-search.html


--
-oj



Show quote
"Stuart Hawkins" <StuartHawk***@discussions.microsoft.com> wrote in message
news:BA78DE3E-A2E3-4E6C-9D88-2B9ABD99F406@microsoft.com...
> Hi,
> I have a Select query I have put in a stored procedure.  What I would
> really
> like to do is pass it parameters that tell it not only the values to
> filter
> by but the fields to filter on too (choice of three including combination
> of
> more than one).
>
> Cannot figure it out!  Any help welcome.
>
> SP eventually called by VB6 program
> Have already sucesfully passed it date paramaeters
>
> Thanks!
Author
2 Jul 2005 2:18 PM
Stuart Hawkins
Brilliant.  Chers mate.

Show quote
"oj" wrote:

> You're looking for dynamic query.
>
> I suggest you take a look at Erland's article for pro/cons.
>
> http://www.sommarskog.se/dyn-search.html
>
>
> --
> -oj
>
>
>
> "Stuart Hawkins" <StuartHawk***@discussions.microsoft.com> wrote in message
> news:BA78DE3E-A2E3-4E6C-9D88-2B9ABD99F406@microsoft.com...
> > Hi,
> > I have a Select query I have put in a stored procedure.  What I would
> > really
> > like to do is pass it parameters that tell it not only the values to
> > filter
> > by but the fields to filter on too (choice of three including combination
> > of
> > more than one).
> >
> > Cannot figure it out!  Any help welcome.
> >
> > SP eventually called by VB6 program
> > Have already sucesfully passed it date paramaeters
> >
> > Thanks!
>
>
>
Author
2 Jul 2005 5:14 AM
David Gugick
Stuart Hawkins wrote:
> Hi,
> I have a Select query I have put in a stored procedure.  What I would
> really like to do is pass it parameters that tell it not only the
> values to filter by but the fields to filter on too (choice of three
> including combination of more than one).
>
> Cannot figure it out!  Any help welcome.
>
> SP eventually called by VB6 program
> Have already sucesfully passed it date paramaeters
>
> Thanks!

Or you can use something like the following if the permutations are
limited:

Select
  col1
From
  dbo.MyTable
Where
  (col1 = @param1 or @param1 is null)
and
  (col2 = @param2 or @param2 is null)
and
  (col3 = @param3 or @param3 is null)

You'll have to verify performance is adequate by testing all 6
combinations and looking at the execution plans.

--
David Gugick
Quest Software
www.imceda.com
www.quest.com
Author
3 Jul 2005 6:58 AM
Brian Selzer
On my database, this construct always performs a clustered index scan,
regardless of the values of the parameters.  There are many indexes on the
table, but the optimizer does not choose them.

Show quote
"David Gugick" <david.gugick-nospam@quest.com> wrote in message
news:uqC9qTsfFHA.3256@TK2MSFTNGP12.phx.gbl...
> Stuart Hawkins wrote:
> > Hi,
> > I have a Select query I have put in a stored procedure.  What I would
> > really like to do is pass it parameters that tell it not only the
> > values to filter by but the fields to filter on too (choice of three
> > including combination of more than one).
> >
> > Cannot figure it out!  Any help welcome.
> >
> > SP eventually called by VB6 program
> > Have already sucesfully passed it date paramaeters
> >
> > Thanks!
>
> Or you can use something like the following if the permutations are
> limited:
>
> Select
>   col1
> From
>   dbo.MyTable
> Where
>   (col1 = @param1 or @param1 is null)
> and
>   (col2 = @param2 or @param2 is null)
> and
>   (col3 = @param3 or @param3 is null)
>
> You'll have to verify performance is adequate by testing all 6
> combinations and looking at the execution plans.
>
> --
> David Gugick
> Quest Software
> www.imceda.com
> www.quest.com
>
Author
3 Jul 2005 5:18 PM
David Gugick
That can happen. If you're scanning a non-clustered index, it's a bit better
than a clustered index scan or table scan. It should provide better
performance than using coalesce and may be the only choice before heading to
dynamic sql. But as you've seen performance testing all your queries is
important in determining whether they operate in a manner acceptable to a
server.

--
David Gugick
Quest Software
www.imceda.com
www.quest.com

Show quote
"Brian Selzer" <br***@selzer-software.com> wrote in message
news:uoZ2Sy5fFHA.3584@TK2MSFTNGP10.phx.gbl...
> On my database, this construct always performs a clustered index scan,
> regardless of the values of the parameters.  There are many indexes on the
> table, but the optimizer does not choose them.
>
> "David Gugick" <david.gugick-nospam@quest.com> wrote in message
> news:uqC9qTsfFHA.3256@TK2MSFTNGP12.phx.gbl...
>> Stuart Hawkins wrote:
>> > Hi,
>> > I have a Select query I have put in a stored procedure.  What I would
>> > really like to do is pass it parameters that tell it not only the
>> > values to filter by but the fields to filter on too (choice of three
>> > including combination of more than one).
>> >
>> > Cannot figure it out!  Any help welcome.
>> >
>> > SP eventually called by VB6 program
>> > Have already sucesfully passed it date paramaeters
>> >
>> > Thanks!
>>
>> Or you can use something like the following if the permutations are
>> limited:
>>
>> Select
>>   col1
>> From
>>   dbo.MyTable
>> Where
>>   (col1 = @param1 or @param1 is null)
>> and
>>   (col2 = @param2 or @param2 is null)
>> and
>>   (col3 = @param3 or @param3 is null)
>>
>> You'll have to verify performance is adequate by testing all 6
>> combinations and looking at the execution plans.
>>
>> --
>> David Gugick
>> Quest Software
>> www.imceda.com
>> www.quest.com
>>
>
>
Author
2 Jul 2005 9:55 AM
--CELKO--
You need to learn that fields and columns are totally different
concepts.  Also a slect is done in parallel while the term filter is
used in sequential file system for record processing. This will let you
skip parameters by passing a NULL.

SELECT a ,b,c,d, ..
  FROM Foobar
WHERE  a = COALESCE (@parm_a, a)
   AND b = COALESCE (@parm_b, b)
   AND c = COALESCE (@parm_c, c);

And of course you did not really mean that  you want to pick "the
fields [sic] to filter on too" on the fly since that would violate
basic software engineering principles about coupling and cohesion,
which is far, far more fundamental than SQL programming.

AddThis Social Bookmark Button