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
 General SQL Server Forums
 New to SQL Server Programming
 problem select with IF

Author  Topic 

eugz
Posting Yak Master

210 Posts

Posted - 2012-08-03 : 10:38:22
Hi All.
I would like to create select statment with IF.
select
if Option_Id in (1722, 1725, 1727, 1728)
begin
select
(Option_Id
,Option
,OptionCode
,'D')
else
select
(Option_Id
,Option
,OptionCode
,'P')
end
from Options
where Type = 'AD'


What is wrong? How to fix?

Thanks.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-03 : 10:43:30
SQL syntax does not let you use an IF condition the way you are trying to do.Change it to one of the following.
-- THIS	
IF Option_Id IN (1722, 1725, 1727, 1728)
BEGIN
SELECT
Option_Id, [OPTION], OptionCode, 'D'
FROM
Options
WHERE
TYPE = 'AD'

END
ELSE
BEGIN
SELECT
Option_Id, [OPTION], OptionCode, 'P'
FROM
Options
WHERE
TYPE = 'AD'
END
--- OR THIS
SELECT
Option_Id, [OPTION], OptionCode,
CASE WHEN Option_Id IN (1722, 1725, 1727, 1728) THEN 'D' ELSE 'P' END
FROM
Options
WHERE
TYPE = 'AD'
Go to Top of Page

eugz
Posting Yak Master

210 Posts

Posted - 2012-08-03 : 10:50:38
Hi sunitabeck. Thanks a lot.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-03 : 12:15:11
You are very welcome.)
Go to Top of Page
   

- Advertisement -