Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
| Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 11th, 2005, 02:56 PM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Stored Procedure Help

How can I turn the below basic procedure into one that will accept the various options (below) and return results. Obviously, as it stands now, if I leave a variable blank it will not return results, same if I set it to null.

@searchterm must have at lease one character or word.
@Category must accept an integer or return all records if left blank.
@Subcategory same as above.
@Submitter same as above.

Thanks in advance for your help!


CREATE PROCEDURE SearchDocument

(
@SearchTerm char (200),
@Category int,
@Subcategory int,
@SubmitterID int
)

as


SELECT dbo.tblDocument.DocumentID,
       dbo.tblDocument.Title,
       dbo.tblSubmitter.SubmitterName

FROM dbo.tblDocument join dbo.tblSubmitter
      on (tbldocument.submitterid = tblsubmitter.submitterid)

where freetext (document, @searchterm) and
        (dbo.tblDocument.SubCategoryID = @subcategory or
        dbo.tblDocument.CategoryID = @category or
        dbo.tblDocument.SubmitterID = @submitterID)
 
Old September 11th, 2005, 03:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Use what I've called the "coalesce trick". Use a parameter = NULL as a "don't care" value.

Your WHERE expression then becomes (e.g.):
Code:
    ... WHERE CategoryID = COALESCE(@Category, CategoryID) ...
When the parameter @Category is NULL, then the coalesce function returns the value of the 'CategoryID' column. Comparing it to itself is always true, so this is a No-op, i.e "don't care" what the 'Category' value is. When @Category is not NULL, then the compare is to the parameter value.


Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 10th, 2006, 08:02 AM
Registered User
 
Join Date: Sep 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello, this doesnt seem to work for me (With regards to Coalesce)

I am trying to return the customer row, which matches the text. Their are 3 parameters, and these should be optional. Here is my code, can someone point me in the right direction please

ALTER PROCEDURE CustomerSearch
(
@CompanyName varchar(50) = null,
@PostCode varchar(50) = null,
@ContactName varchar(50) = null
)
AS
BEGIN
SET NOCOUNT ON
Select * FROM Customers WITH (NOLOCK)
WHERE CompanyName LIKE COALESCE(@CompanyName + '%', Customers.CompanyName) OR (@CompanyName = null)
AND PostCode LIKE COALESCE(@PostCode + '%', Customers.PostCode) OR (PostCode = null)
AND ContactName LIKE COALESCE(@ContactName + '%', Customers.ContactName) OR (@ContactName = null);


End
Return


Many thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
stored procedure prashant_telkar SQL Server 2000 1 July 9th, 2007 07:57 AM
Stored Procedure jezywrap SQL Server ASP 1 January 3rd, 2007 12:29 AM
stored procedure kdm260 SQL Server 2000 2 June 19th, 2006 04:45 PM
Stored Procedure rajanikrishna SQL Server 2000 0 July 18th, 2005 05:01 AM
Help About Stored Procedure zhuge6 BOOK: ASP.NET Website Programming Problem-Design-Solution 3 May 20th, 2005 09:27 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.