Solved

Variable in Case Expression

Posted on 2010-08-20
6
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

752 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