SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Union Queries/Store procedure


Union Queries/Store procedure

Author
Message
DaveK2014
DaveK2014
SSC-Enthusiastic
SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)

Group: General Forum Members
Points: 146 Visits: 64
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?
Dana
Dana
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5992 Visits: 3453
Remove the quotes from around the parameters. '@Criteria1' should be @Criteria1
DaveK2014
DaveK2014
SSC-Enthusiastic
SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)

Group: General Forum Members
Points: 146 Visits: 64
thanks didn't even register that they were left over from typing text in manually to test.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64247 Visits: 17974
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/

_______________________________________________________________

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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
DaveK2014
DaveK2014
SSC-Enthusiastic
SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)

Group: General Forum Members
Points: 146 Visits: 64
Thanks I will take a look.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search