You are here: Re: Using IF inside SELECT ? « MsSQL Server « IT news, forums, messages
Re: Using IF inside SELECT ?

Posted by Hugo Kornelis on 10/02/06 11:45

On 19 Apr 2006 07:07:47 -0700, Igor wrote:

>Is there possibility to use IF conditions inside SELECT statements?
>
>For example, can i write something like this:
>CREATE PROCEDURE [search]
>(
>@OPTION int,
>@KEYWORD nvarchar(40)
>)
>AS
>BEGIN
> SELECT id FROM projects WHERE title LIKE @KEYWORD IF (@OPTION = 1)
>THEN (OR description LIKE @KEYWORD)
>END
>
>or am i limited to this:
>...
>BEGIN
> IF @OPTION = 1
> SELECT id FROM projects WHERE title LIKE @KEYWORD OR description LIKE
>@KEYWORD
> ELSE
> SELECT id FROM projects WHERE title LIKE @KEYWORD
>END

Hi Igor,

The latter will probably perform best. But if you prefer to have it in
one query, you can also use

SELECT id
FROM projects
WHERE title LIKE @keyword
AND ( @option <> 1 OR description LIKE @keyword )

For more complex cases, check out the CASE expression in Books Online.

--
Hugo Kornelis, SQL Server MVP

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация