Solved

Using a SELECT statement inside of a CASE

Posted on 2016-11-16
5
36 Views
Last Modified: 2016-11-16
I have a table variable called @Results with a bunch of data in it.  One of the columns is called Info.

I'm trying to update the data in this column using the following:

                        Set Info =

                        CASE WHEN [@Results].Info = 'Other (Please explain)'
                        THEN
                        ('Other - ' + SELECT InformationTrackings.OtherInformation FROM InformationTrackings
                                WHERE  InformationTrackings.TrackingId = [@Results].Id)
                        ELSE Info
                        END

This seems to work OK if I leave off the SELECT part of the statement, and just have 'Other-'.  But when I add the SELECT part it doesn't seem to be the correct syntax.
0
Comment
Question by:delmarvamonkey
[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
5 Comments
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41889955
If that returns only one row make the concatenation inside the SELECT:
CASE WHEN [@Results].Info = 'Other (Please explain)'
       THEN SELECT 'Other - ' + InformationTrackings.OtherInformation 
            FROM InformationTrackings 
            WHERE  InformationTrackings.TrackingId = [@Results].Id
        ELSE Info
END

Open in new window

0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41889972
could you please post the entire query.
0
 

Author Comment

by:delmarvamonkey
ID: 41889978
I'm getting an error on "SELECT" and "[@Results].Id" and "ELSE"

SELECT says "incorrect syntax near SELECT"
[@Results].Id says The multi part identifier could not be bound
ELSE says Incorrect syntax near ELSE

                        UPDATE @Results

                        SET Info =

                        CASE WHEN [@Results].Info = 'Other (Please explain)'
                                 THEN SELECT 'Other - ' + InformationTrackings.OtherInformation
                                          FROM InformationTrackings
                                          WHERE  InformationTrackings.MedicTrackingId = [@Results].Id
                                    ELSE Info
                        END
0
 
LVL 50

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41889995
Then you don't even need the CASE statement. It's more simple than that:
UPDATE R
SET R.Info = 'Other - ' + InformationTrackings.OtherInformation 
FROM @Results R
	INNER JOIN InformationTrackings on InformationTrackings.MedicTrackingId = R.Id
WHERE R.Info = 'Other (Please explain)'

Open in new window

0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41889996
And like the above solution you'll only update the rows that really need to be updated. Much faster.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

705 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