Sunday, February 19, 2012

Convert If-Else into Case statement

Can I convert this IF-ELSE statement into a CASE statement? Thanks for
your help!
declare @.source varchar(150)
declare @.destination varchar(150)
if (select @.@.servername) = 'A'
begin
set @.source = 'folder1\file.txt'
set @.destination = 'folder2'
end
else
if (select @.@.servername) = 'B'
begin
set @.source = 'folder3\file2.txt'
set @.destination = 'folder4'
end
else
if (select @.@.servername) = 'C'
begin
set @.source = 'folder4\.*'
set @.destination = 'folder4'
end
*** Sent via Developersdex http://www.examnotes.net ***Give this a try...
declare @.source varchar(150)
declare @.destination varchar(150)
select @.source = case @.@.servername
when 'A' then 'folder1\file.txt'
when 'B' then 'folder3\file2.txt'
when 'C' then 'folder4\.*'
end
, @.destination = case @.@.servername
when 'A' then 'folder2'
when 'B' then 'folder4'
when 'C' then 'folder4'
end
--Brian
(Please reply to the newsgroups only.)
"Test Test" <farooqhs_2000@.yahoo.com> wrote in message
news:OMYWbhTuFHA.460@.TK2MSFTNGP15.phx.gbl...
> Can I convert this IF-ELSE statement into a CASE statement? Thanks for
> your help!
>
> declare @.source varchar(150)
> declare @.destination varchar(150)
> if (select @.@.servername) = 'A'
> begin
> set @.source = 'folder1\file.txt'
> set @.destination = 'folder2'
> end
> else
> if (select @.@.servername) = 'B'
> begin
> set @.source = 'folder3\file2.txt'
> set @.destination = 'folder4'
> end
> else
> if (select @.@.servername) = 'C'
> begin
> set @.source = 'folder4\.*'
> set @.destination = 'folder4'
> end
>
>
> *** Sent via Developersdex http://www.examnotes.net ***|||declare @.source varchar(150)
declare @.destination varchar(150)
SELECT @.source =
CASE @.@.servername
WHEN 'A' THEN 'folder1\file.txt'
WHEN 'B' THEN 'folder3\file2.txt'
WHEN 'C' THEN 'folder\4.*'
END,
@.destination =
CASE @.@.servername
WHEN 'A' THEN 'folder2\'
WHEN 'B' THEN 'folder4\'
WHEN 'C' THEN 'folder4'
END|||Heh,
Sorry for the duplication, I guess I was typing my response whilst
Brian was posting his!
Still, we both posted the same thing so I guess that's confirmation it
works!|||THANX A LOT!!!!
*** Sent via Developersdex http://www.examnotes.net ***|||...or we're both just feeling lucky this morning. ;)
--Brian
(Please reply to the newsgroups only.)
"RobJH" <robertjhenderson@.gmail.com> wrote in message
news:1126709760.080074.261130@.g14g2000cwa.googlegroups.com...
> Heh,
> Sorry for the duplication, I guess I was typing my response whilst
> Brian was posting his!
> Still, we both posted the same thing so I guess that's confirmation it
> works!
>

No comments:

Post a Comment