Solved

Need help with CASE statement in select statement

Posted on 2015-02-17
8
113 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Split data on commas and insert into another table in separate records 26 70
Nested Case statement 4 37
SQL Server / Update DB? 22 36
TSQL XML Namespaces 7 22
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…
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…
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.

839 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