Solved

Variable in Case Expression

Posted on 2010-08-20
6
254 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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:ScottPletcher
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now