Solved

Need help with CASE statement in select statement

Posted on 2015-02-17
8
114 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
[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
  • 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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
 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL query for highest sequence 4 64
SQL Server Trigger 8 37
SQL Server Resume 5 46
Need SSIS project 2 30
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…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

733 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