Solved

Using a SELECT statement inside of a CASE

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

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 18

Expert Comment

by:Pawan Kumar Khowal
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 45

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 45

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

759 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

23 Experts available now in Live!

Get 1:1 Help Now