Tuesday, March 27, 2012

Help with CASE T-SQL

Anyone have much experience with SQL Server T-SQL CASE statement?
I have some code that someone else wrote that looks like this:
SELECT m.messageID, m.isTop,
CASE @.Mode
WHEN 10 THEN m.subject
WHEN 12 THEN p.userID
END
FROM Messages m
INNER JOIN People p ON m.userID=p.userID
WHERE [blah][blah][blah].
What I need to do is return something that is not the userID, but still have
that name. This did not work:
SELECT m.messageID, m.isTop,
CASE @.Mode
WHEN 10 THEN m.subject
WHEN 12 THEN pr.userName As userID
END
FROM Messages m
INNER JOIN People p ON m.userID=p.userID
INNER JOIN Personalize pr ON m.userID=pr.userID
WHERE [blah][blah][blah].
SQL dies on the "As"... (or, if I leave out the "As", it dies on "userID")
Any idea how I can do this?
Thanks,
OwenCASE @.Mode
WHEN 10 THEN m.subject
WHEN 12 THEN pr.userName
END As UserID
"Owen Mortensen" <ojm.NO_SPAM@.acm.org> wrote in message
news:OWsmKlxPGHA.3460@.TK2MSFTNGP15.phx.gbl...
> Anyone have much experience with SQL Server T-SQL CASE statement?
> I have some code that someone else wrote that looks like this:
> SELECT m.messageID, m.isTop,
> CASE @.Mode
> WHEN 10 THEN m.subject
> WHEN 12 THEN p.userID
> END
> FROM Messages m
> INNER JOIN People p ON m.userID=p.userID
> WHERE [blah][blah][blah].
> What I need to do is return something that is not the userID, but still
> have that name. This did not work:
> SELECT m.messageID, m.isTop,
> CASE @.Mode
> WHEN 10 THEN m.subject
> WHEN 12 THEN pr.userName As userID
> END
> FROM Messages m
> INNER JOIN People p ON m.userID=p.userID
> INNER JOIN Personalize pr ON m.userID=pr.userID
> WHERE [blah][blah][blah].
> SQL dies on the "As"... (or, if I leave out the "As", it dies on "userID")
> Any idea how I can do this?
> Thanks,
> Owen
>|||Thanks. This variation on that theme actually worked:
CASE @.Mode WHEN 10 THEN m.subject END As subject,
CASE @.Mode WHEN 12 THEN pr.userName END As UserID
"Norman Yuan" <NotReal@.NotReal.not> wrote in message
news:utqlB9xPGHA.1216@.TK2MSFTNGP14.phx.gbl...
> CASE @.Mode
> WHEN 10 THEN m.subject
> WHEN 12 THEN pr.userName
> END As UserID
>
> "Owen Mortensen" <ojm.NO_SPAM@.acm.org> wrote in message
> news:OWsmKlxPGHA.3460@.TK2MSFTNGP15.phx.gbl...
>

No comments:

Post a Comment