Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 case statements

Author  Topic 

darenkov
Yak Posting Veteran

90 Posts

Posted - 2005-02-15 : 03:39:13
I am trying to evaluate a variable in an SQL statment with a simple case statement below. Basically if the input variable is = 15 I don't want anything to happen, else I want an extra line added (please see below). Everything else works fine in the proc except for the case statement


CREATE PROCEDURE uspSearch(

@suburbtown varchar(30) = NULL,
@accountant_type int = NULL,
@keyword varchar(25) = NULL,
@state varchar(3) = NULL

)

AS

BEGIN

SELECT DISTINCT B.business_id, B.rank, B.business_name, B.suburb_town, B.postcode, B.phone,
B.fax, B.status, BF.description

FROM BUSINESS B

left join BUSINESSFULL BF ON B.business_id = BF.business_id

left join SERVICESOFFERED SO on B.business_id = SO.business_id

left join SERVICES S on S.service_id = SO.service_id

WHERE

CASE
WHEN @accountant_type = 15 THEN 0 ELSE
'(S.service_id = @accountant_type) AND'
END
(BF.description like @keyword + '%' OR @keyword is NULL) AND
(B.suburb_town = @suburbtown OR B.postcode = @suburbtown OR
@suburbtown is NULL) AND (B.state = @state OR @state is NULL) AND
(B.status >=3 )
END

LarsG
Constraint Violating Yak Guru

284 Posts

Posted - 2005-02-15 : 04:12:19
[code]
WHERE @accountant_type in(15,s.service_id)
AND (BF.description like @keyword + '%' OR @keyword is NULL)
AND (B.suburb_town = @suburbtown OR B.postcode = @suburbtown OR @suburbtown is NULL)
AND (B.state = @state OR @state is NULL)
AND (B.status >=3 )
[/code]
Go to Top of Page

darenkov
Yak Posting Veteran

90 Posts

Posted - 2005-02-15 : 04:34:27
Thanks Lars that works great!
Go to Top of Page
   

- Advertisement -