Solved

Using a SELECT statement inside of a CASE

Posted on 2016-11-16
5
28 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 49

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 28

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 49

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 49

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

713 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