Solved

update sql query with case statement

Posted on 2016-07-29
1
37 Views
Last Modified: 2016-07-29
I have the following select query

select
w.login,
w.RolesSetThru,
e.Authenticity as eAuthenticity,
a.Authenticity as aAuthenticity ,
case when e.Authenticity in (1,3) then 'AD'
     when a.Authenticity in (1,3) then 'AD'
else
      'ERMXref'
end            as Authenticity  
from  controls..WrkUser w
left outer join controls..WrkERMGroupMember e on
 w.Login = e.Login
left outer join controls..WrkADGroupMember a on
 w.Login = a.Login

I want to update w.RolesSetThru column with the values that I get from case statement. Can someone please show me how I can get that done
0
Comment
Question by:PratikShah111
1 Comment
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 41735112
You can't do a SELECT and UPDATE in the same statement, so you'll have to borrow from this SELECT  and write an UPDATE.

Give this a whirl..
UPDATE w
SET w.RolesSetThru = case 
   when e.Authenticity in (1,3) then 'AD'
   when a.Authenticity in (1,3) then 'AD'
   else  'ERMXref' end       
FROM  controls..WrkUser w
   LEFT JOIN controls..WrkERMGroupMember e on w.Login = e.Login
   LEFT JOIN controls..WrkADGroupMember a on w.Login = a.Login

Open in new window

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 Select JOIN table 2 24
SQL Server Deadlocks 12 51
SQL Server stored proc 2 14
SQL - insert empty rows into output results 11 27
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

863 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

24 Experts available now in Live!

Get 1:1 Help Now