Solved

Need help with CASE statement in select statement

Posted on 2015-02-17
8
108 Views
Last Modified: 2015-02-17
I am trying to use a case statement in my UPDATE statement below in order to set the variable CEDM_ENVT_CODE. I have not had any success, and need to ask for some expert help.
Can someone help me out here?

DECLARE @ENVT_CODE AS CHAR(10)
DECLARE @PRODUCT_SUITE_CODE AS CHAR(10)

SET @ENVT_CODE = 'BOL'
SET @PRODUCT_SUITE_CODE = 'U2FD'

UPDATE STAGER_BLLD_ASSOCIATE_USERS
SET
PSTE_CODE = @ENVT_CODE
CEDM_ENVT_CODE = (CASE WHEN POLM_USER_ID IN ( SELECT POLM_USER_ID FROM STAGER_BLLD_ENVT_USERIDS  )
THEN @PRODUCT_SUITE_CODE
ELSE NULL
END)
0
Comment
Question by:brgdotnet
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40615627
there is a comma missing between the 2 fields being set, see line 9 below
DECLARE @ENVT_CODE AS char(10)
DECLARE @PRODUCT_SUITE_CODE AS char(10)

SET @ENVT_CODE = 'BOL'
SET @PRODUCT_SUITE_CODE = 'U2FD'

UPDATE STAGER_BLLD_ASSOCIATE_USERS
SET PSTE_CODE = @ENVT_CODE
  , CEDM_ENVT_CODE = (CASE
          WHEN POLM_USER_ID IN (
                      SELECT
                            POLM_USER_ID
                      FROM STAGER_BLLD_ENVT_USERIDS
                ) THEN @PRODUCT_SUITE_CODE
          ELSE NULL
    END)

Open in new window


or is it something more that you need?
0
 
LVL 11

Accepted Solution

by:
b_levitt earned 375 total points
ID: 40615646
Honestly, just do it in two updates:

UPDATE STAGER_BLLD_ASSOCIATE_USERS
 SET
 PSTE_CODE = @ENVT_CODE,
 CEDM_ENVT_CODE = @PRODUCT_SUITE_CODE
 where POLM_USER_ID IN ( SELECT POLM_USER_ID FROM STAGER_BLLD_ENVT_USERIDS  )
 
 
 UPDATE STAGER_BLLD_ASSOCIATE_USERS
 SET
 PSTE_CODE = @ENVT_CODE,
 CEDM_ENVT_CODE = @PRODUCT_SUITE_CODE
 where POLM_USER_ID not IN ( SELECT POLM_USER_ID FROM STAGER_BLLD_ENVT_USERIDS  )
 

Open in new window

You could certainly combine those using a join but I figured the above might be more in your wheelhouse.  But if you're feeling dangerous:
 
update sbau
set PSTE_CODE = @ENVT_CODE,
CEDM_ENVT_CODE = case when sbeu.polm_user_id is null then null else @ENVT_CODE end
from STAGER_BLLD_ASSOCIATE_USERS sbau
left join (select distinct polm_user_id from STAGER_BLLD_ENVT_USERIDS) sbeu
  on sbau.polm_user_id=sbeu.polm_user_id  
 

Open in new window

0
 
LVL 11

Assisted Solution

by:b_levitt
b_levitt earned 375 total points
ID: 40615657
@PortletPaul,

Tip of the hat for catching comma, wag of the finger for the nested query in the select :).  OK for one offs, but you don't want to be teaching any bad habits.
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 125 total points
ID: 40615663
Oh, I agree, I wasn't certain what was being asked.

I prefer to 2 updates you suggest

( except line 11 should be setting the value to NULL I think )
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 11

Assisted Solution

by:b_levitt
b_levitt earned 375 total points
ID: 40615677
Thank you sir.

Fixed query is:

UPDATE STAGER_BLLD_ASSOCIATE_USERS
 SET
 PSTE_CODE = @ENVT_CODE,
 CEDM_ENVT_CODE = @PRODUCT_SUITE_CODE
 where POLM_USER_ID IN ( SELECT POLM_USER_ID FROM STAGER_BLLD_ENVT_USERIDS  )
 
 
 UPDATE STAGER_BLLD_ASSOCIATE_USERS
 SET
 PSTE_CODE = @ENVT_CODE,
 CEDM_ENVT_CODE = null
where POLM_USER_ID not IN ( SELECT POLM_USER_ID FROM STAGER_BLLD_ENVT_USERIDS  )

Open in new window

0
 
LVL 2

Author Comment

by:brgdotnet
ID: 40615724
Yes, I forgot the Comma Portlet Paul. I am not sure about using two updates, I am still trying to digest breaking it up into two statements.
0
 
LVL 2

Author Closing Comment

by:brgdotnet
ID: 40615753
I gave Portlet Paul good points, because he caught the error, and b levitt so graciously accomdated the change based upon portelt pauls answer. Thank you guys!!!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40615754
Beyond Paul catching the comma, do you have any other needs not stated in this question?  

>in order to set the variable CEDM_ENVT_CODE.
btw I don't see a @ before CEDM_ENVT_CODE, so it's not a variable.  Is it a column on table STAGER_BLLD_ASSOCIATE_USERS, or is there a typo somewhere else?

btw I have an article out there on SQL Server CASE Solutions, with a wompload of examples, if it helps.
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

Title # Comments Views Activity
MS SQL Backup 24 70
SQL server 2008 SP4 29 33
separate column 24 20
Dynamic sql query that use Nvarchar and exceed 4000 characters 22 23
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

758 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

17 Experts available now in Live!

Get 1:1 Help Now