Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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)
Reply With Quote
  #2 (permalink)  
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
Reply With Quote
  #3 (permalink)  
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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:06 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.