Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored Procs and Conditional Where Clause 1

Status
Not open for further replies.

Smeat

Programmer
Mar 27, 2004
193
GB
Hi All

Is it possible to have a conditional where clause in a stored procedure?

For example:

Code:
CREATE PROCEDURE dbo.EnquiryList
  @LocationID int

AS

SELECT ID, EnquiryDate
FROM
Enquiry

IF @LocationID > 0 THEN
  BEGIN
    WHERE LocationID = @LocationID
  END
ELSE
  BEGIN
    WHERE LocationID IS NULL
  END

Regards

Smeat
 
Code:
CREATE PROCEDURE dbo.EnquiryList
  @LocationID int

AS

SELECT ID, EnquiryDate
FROM
Enquiry
WHERE ISNULL(LocationID,-1) = ISNULL(@LocationID,-1)
Where -1 is value you are very sure not presented in your data.
Not tested

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Hi bborissov

Thanks for your response.

I have one further question:

Is it also possible to do something like the following:

Code:
CREATE PROCEDURE dbo.EnquiryList
  @LocationID int,
  @ExecutiveID int

AS

SELECT ID, EnquiryDate
FROM
Enquiry

WHERE

  EnquiryDate IS NOT NULL

IF @LocationID > 0 THEN
  BEGIN
    AND LocationID = @LocationID
  END

IF @ExecutiveID > 0 THEN
  BEGIN
    AND ExecutiveID = @ExecutiveID
  END

What I need to do is create a stored procedure that accepts filter criteria that may or may not be specified. If the criteria is specified then they must be used to filter the results, else they should be ignored.

Regards

Smeat
 
This is not the same query. You want ALL records if some of the parameter IS NULL or matching records if the parameters is not NULL:
Code:
CREATE PROCEDURE dbo.EnquiryList
  @LocationID int,
  @ExecutiveID int
AS

SELECT ID, EnquiryDate
FROM
Enquiry
WHERE EnquiryDate IS NOT NULL                            AND
      (@LocationID IS NULL OR LocationID = @LocationID)  AND
      (@ExecutiveID IS NULL OR ExecutiveID= @ExecutiveID)

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
The solution provided by bborissov is the way to go.

Although it is possible to create dynamic sql in a stored procedure by using the sp_sqlexec stored procedure, you should avoid using it.

Greetz,

Geert


Geert Verhoeven
Consultant @ Ausy Belgium

My Personal Blog
 
Thanks again for your responses though i'm not sure this does what I need.

My dilema is that I must ONLY apply a filter IF one is specified.

For example, if @LocationID is null then I should not apply this as a filter. If it is not null then I should apply this as a filter.

Code:
WHERE
  EnquiryDate IS NOT NULL

IF @LocationID IS NULL
  -- Don't apply filter
ELSE
  -- Do apply filter
  BEGIN
    AND LocationID = @LocationID
  END

Regards

Smeat
 
Smeat,
take a gooood look in my WHERE clause. If you didn't pass @LocationID as parameter it is NULL by default, so
Code:
WHERE (@LocationID IS NULL  OR LocationID =  @LocationID)
will works for ALL records, because first part will be satisfied (@LocationID IS NULL). If you pass a value to @LocationID then second part of that condition will work and you will have all records that match the condition:
Code:
LocationID =  @LocationID

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Hi bborissov

Sorry, I didn't read your reply properly first time around.

I'll try your solution first thing in the morning.

Thanks again for your help

Smeat

 
Hi bborissov

I tried out your suggestion and it does exactly what I needed, have a star for taking the time to help.

Regards

Smeat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top