Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Variable in Case Expression

Posted on 2010-08-20
6
Medium Priority
?
320 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 1500 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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 70

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

916 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