p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > Database > SQL Language
I forgot my password Register Now
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 11th, 2005, 03: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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old September 11th, 2005, 04:09 PM
Friend of Wrox
Points: 2,591, Level: 21
Points: 2,591, Level: 21 Points: 2,591, Level: 21 Points: 2,591, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 0 Times in 0 Posts
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old October 10th, 2006, 09: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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
stored procedure prashant_telkar SQL Server 2000 1 July 9th, 2007 08: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 05:45 PM
Stored Procedure rajanikrishna SQL Server 2000 0 July 18th, 2005 06:01 AM
Help About Stored Procedure zhuge6 BOOK: ASP.NET Website Programming Problem-Design-Solution 3 May 20th, 2005 10:27 AM



All times are GMT -4. The time now is 07:25 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc