Solved

Using a SELECT statement inside of a CASE

Posted on 2016-11-16
5
19 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
  • 3
5 Comments
 
LVL 46

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 24

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 46

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 46

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

920 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

15 Experts available now in Live!

Get 1:1 Help Now