Solved

Variable in Case Expression

Posted on 2010-08-20
6
272 Views
Last Modified: 2012-06-27
I have this scenario : In SQL 2008 I run query  select statement in which  want to declare variable that I  will use in that statement  to allocate value of from subquery, and  then  use this variable in case expression.
Something as
Declare @GetTeamDesc NVARCHAR(100)
Select field1,field2,(subquery_getTeamDesc)as TeamDesc,field3
Now I want to use value of subqery in case when…expression.
However subqery is big and I don’t want to repeate it in case when can I do something as

I want something like this:
Select field1,
field2,
Set @GetTeamDesc= (Subquery_getTeamDesc)
Case
When @GetTeamDesc like %XXX% Then
‘Team_One’
When @GetTeamDesc  like %YYY% then
‘Team_Two’
Else
@GetTeamDesc
End as Team_Desc ,
Filed3
Is this posible I am getting error:
Incorrect syntax near the keyword 'SET' at line Set @GetTeamDesc = (Subquery_getTeamDesc)

0
Comment
Question by:Taras
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:vdr1620
ID: 33488144
You can try this..instead of a Sub Query Try using a CTE ..You cannot assign the Values to a variable while retrieving data

WITH CTE As
(
Your Sub query here
)

SET @GetTeamDesc = CASE WHEN Subquery_getTeamDesc LIKE %XXX% THEN 'TeamOne'
                                                 When Subquery_getTeamDesc  like %YYY% then ‘Team_Two’
                                     ELSE  Subquery_getTeamDesc
                                     END As Team_Desc

Select field1,
field2,
@GetTeamDesc,
Filed3
FROM TABLENAME
0
 

Author Comment

by:Taras
ID: 33488320
Vdr1620:
It means I need to repeat several times all query in case expression. I  Do not see purpose of variables if  It did not shorten process.
If I use CTE as you suggested, would it be better to do it on this way?
WITH CTE As
(
(Your Sub query here )as TeamDesc
)

Select
Field1,
Field2,
Case
When CTE.TeamDesc like %XXX% THEN 'Team_One'
When CTE.TeamDesc like %YYY%  THEN ‘Team_Two’
Else
CTE.TeamDesc
End as Team_Desc
Field3
From CTE

You started with CTE however I don’t see where you use it down.
Is this should be:
Select field1,
field2,
@GetTeamDesc,
Filed3
FROM CTE

0
 
LVL 16

Accepted Solution

by:
vdr1620 earned 500 total points
ID: 33488504
Yes... The query you have will resolve the issue of using Sub query multiple times..Just the 1st part i.e. CTE and Select ..... from CTE  should do it
WITH CTE As
(
(Your Sub query here )as TeamDesc
)

Select 
Field1,
Field2,
Case
When CTE.TeamDesc like %XXX% THEN 'Team_One'
When CTE.TeamDesc like %YYY%  THEN ‘Team_Two’
Else
CTE.TeamDesc
End as Team_Desc
Field3
From CTE

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 16

Expert Comment

by:vdr1620
ID: 33488554
ooops forgot to update it..here you go..by doing as below you will be making use of the query within CTE multiple times
WITH CTE As
(
--Just write your select statement as in your sub query
SELECT ColumnName FROM TableName
)

Select 
Field1,
Field2,
Case
When CTE.ColumnName like %XXX% THEN 'Team_One'
When CTE.ColumnName like %YYY%  THEN ‘Team_Two’
Else
CTE.ColumnName
End as Team_Desc
Field3
From CTE

Open in new window

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 33488897
A derived table may be less overhead here.  That is, add an outer query to the original query.



SELECT field1, field2, CASE
    WHEN getTeamDesc LIKE '...' THEN '...'
    WHEN getTeamDesc LIKE '...' THEN '...'
    ...
    END AS ...
FROM (
    SELECT 
        field1, 
        field2,
         (Subquery_getTeamDesc) AS getTeamDesc,
        Filed3
    FROM ...
) AS derived

Open in new window

0
 

Author Closing Comment

by:Taras
ID: 33694571
Thanks a lot.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question