Solved

Variable in Case Expression

Posted on 2010-08-20
6
283 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Title # Comments Views Activity
Connect to an SQL server ActiveX 10 29
SSAS Store Forecasting data in the cube 1 19
Parse this column 6 27
SQL 2008 R2 problem with DB 9 16
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

828 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