Solved

update sql query with case statement

Posted on 2016-07-29
1
42 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.

856 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