StackTrace
StackTrace

Reputation: 9416

Using CASE WHEN in T-SQL select statement

    SELECT Country, EmployeeName, Name As [FullName], OutPut1, OutPut2, OutPut3, 
    (OutPut1 + OutPut2 + OutPut3) AS Total,
    CASE WHEN Name IS NULL THEN '.........' 
         WHEN Name IS NOT NULL THEN '??????'
         END) 
         AS ParentID
 FROM CTE_Report;

The CASE WHEN in the above statement is giving trouble with the error below. Any one know what i'm missing?

Common table expression defined but not used..

Upvotes: 0

Views: 5061

Answers (3)

StackUser
StackUser

Reputation: 5398

Try this

SELECT Country,
       EmployeeName,
       Name                            AS [FullName],
       OutPut1,
       OutPut2,
       OutPut3,
       ( OutPut1 + OutPut2 + OutPut3 ) AS Total,
       CASE
         WHEN Name IS NULL THEN '.........'
         ELSE '??????'
       END                             AS ParentID
FROM   CTE_Report; 

Upvotes: 0

Vasily
Vasily

Reputation: 5782

Common table expression defined but not used..

this is mean that you create CTE but do not use it, same error as in snapshot below

enter image description here

Upvotes: 1

CatchingMonkey
CatchingMonkey

Reputation: 1391

SELECT Country, EmployeeName, Name As [FullName], OutPut1, OutPut2, OutPut3, 
(OutPut1 + OutPut2 + OutPut3) AS Total,
CASE WHEN Name IS NULL THEN '.........' 
     WHEN Name IS NOT NULL THEN '??????'
     END
     AS ParentID
 FROM CTE_Report;

Its a parenthesis problem.

Upvotes: 0

Related Questions