Union Queries/Store procedure

  • We are trying to set up a stored procedure that will use up to three search criteria to search in two different fields then union them together.

    [dbo].[SP_NAME]

    @Criteria1 varchar (50),

    @Criteria2 varchar (50),

    @Criteria3 varchar (50)

    AS

    select Category,OPT,[Description],Comments,UOM,Item1,Item2,Item3,Item4,Item5,Item6,Item7,Item8,

    Item9,Item10,Major_Group from tblDCOptions

    where

    Description like '@Criteria1' and

    Description like '@Criteria2' and

    Description like '@Criteria3' and

    (InActive is null or InActive = 0 )

    union

    select Category,OPT,[Description],Comments,UOM,Item1,Item2,Item3,Item4,Item5,Item6,Item7,Item8,

    Item9,Item10,Major_Group from tblDCOptions

    where

    Comments like '@Criteria1' and

    Comments like '@Criteria2' and

    Comments like '@Criteria3' and

    (InActive is null or InActive = 0 )

    order by OPT

    If I run this as a sql query so take out the @criteria info and type in items it works fine. If I execute the stored procedure I get nothing.

    Each criteria is something like %Search-item% or if it is blank it is just %%

    Can this not be done in a stored procedure?

  • Remove the quotes from around the parameters. '@Criteria1' should be @Criteria1

  • thanks didn't even register that they were left over from typing text in manually to test.

  • You might want to take a look at this blog post from Gail. She goes over how to handle catch-all type queries. This is very close to that type of situation and reading her post may very well help you improve the performance of your procedure.

    http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/[/url]

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Thanks I will take a look.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply