|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to add condition to where clause?I have a long query and therefore want to avoid using it twice in an if else
structure, while still be able to achieve excluding an id when @id=null(meaning all included) in the where clause or like. i.e. if @id=null theId<>555 Thanks, -- bic WHERE (@id IS NULL OR theId<>555)
On Thu, 22 Jun 2006 11:10:02 -0700, bic <b**@discussions.microsoft.com> wrote: Show quote >I have a long query and therefore want to avoid using it twice in an if else >structure, while still be able to achieve excluding an id when >@id=null(meaning all included) in the where clause or like. >i.e. >if @id=null >theId<>555 > >Thanks, bic wrote:
> I have a long query and therefore want to avoid using it twice in an if else One way is to add:> structure, while still be able to achieve excluding an id when > @id=null(meaning all included) in the where clause or like. > i.e. > if @id=null > theId<>555 > > Thanks, AND (id = @id OR @id IS NULL) If I understood it right.
one way (id = @id or @id is null) another way id = coalesce(@id,id) Hope this helps. If you posted the entire query, it would be easier to understand what you are trying to accomplish.
Pehaps this idea could work for you. (It would require @ID = 0 rather than null) USING NORTHWIND GO DECLARE @Test int SET @Test = 0 SELECT EmployeeID , LastName FROM Employees WHERE ( @Test = CASE @Test WHEN 0 THEN @Test ELSE -1 END OR EmployeeID = @Test ) SET @Test = 5 SELECT EmployeeID , LastName FROM Employees WHERE ( @Test = CASE @Test WHEN 0 THEN @Test ELSE -1 END OR EmployeeID = @Test ) -- Show quoteArnie Rowland, YACE* "To be successful, your heart must accompany your knowledge." *Yet Another Certification Exam "bic" <b**@discussions.microsoft.com> wrote in message news:12381DCD-549E-40D0-B85B-EC9A87AA005C@microsoft.com... >I have a long query and therefore want to avoid using it twice in an if else > structure, while still be able to achieve excluding an id when > @id=null(meaning all included) in the where clause or like. > i.e. > if @id=null > theId<>555 > > Thanks, > -- > bic For your particular situation:
(If you can pass in a 0 instead of a NULL for @ID to get all records -except 555) WHERE ( ( @ID = CASE @ID WHEN 0 THEN @ID ELSE -1 END AND theID <> 555 ) OR EmployeeID = @ID ) -- Arnie Rowland, YACE* "To be successful, your heart must accompany your knowledge." *Yet Another Certification Exam "Arnie Rowland" <ar***@1568.com> wrote in message news:%23N1ZptilGHA.1276@TK2MSFTNGP03.phx.gbl... If you posted the entire query, it would be easier to understand what you are trying to accomplish.Pehaps this idea could work for you. (It would require @ID = 0 rather than null) USING NORTHWIND GO DECLARE @Test int SET @Test = 0 SELECT EmployeeID , LastName FROM Employees WHERE ( @Test = CASE @Test WHEN 0 THEN @Test ELSE -1 END OR EmployeeID = @Test ) SET @Test = 5 SELECT EmployeeID , LastName FROM Employees WHERE ( @Test = CASE @Test WHEN 0 THEN @Test ELSE -1 END OR EmployeeID = @Test ) -- Arnie Rowland, YACE* "To be successful, your heart must accompany your knowledge." *Yet Another Certification Exam Show quote "bic" <b**@discussions.microsoft.com> wrote in message news:12381DCD-549E-40D0-B85B-EC9A87AA005C@microsoft.com... >I have a long query and therefore want to avoid using it twice in an if else > structure, while still be able to achieve excluding an id when > @id=null(meaning all included) in the where clause or like. > i.e. > if @id=null > theId<>555 > > Thanks, > -- > bic Perhaps I did not make myself clear; I am trying to implement the following,
AND theid= CASE WHEN @id=NULL THEN <>55 ELSE @id END can someone offer help to fix my syntax. Thanks. -- Show quotebic "bic" wrote: > I have a long query and therefore want to avoid using it twice in an if else > structure, while still be able to achieve excluding an id when > @id=null(meaning all included) in the where clause or like. > i.e. > if @id=null > theId<>555 > > Thanks, > -- > bic |
|||||||||||||||||||||||