Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
| Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 3rd, 2008, 03:20 PM
Friend of Wrox
 
Join Date: Oct 2004
Location: delhi, delhi, India.
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default multiple column update query with Case

Hii Alll
Is it possible to update multiple column based on case statement??
employeeTable
_________________
empID FirstName LastName Address
1 N N N
2 N N N
3 N N N

ValueTable
----------------------
empID NewVAlue ID
1 'FN' 10
1 'LN' 20
2 'AA' 10
2 'BB' 30
3 'AA1' 10
3 'BB1' 20
3 'cc1' 30

updated employeeTable output result should be
_________________
empID FirstName LastName Address
1 FN LN N
2 AA N BB
3 AA1 BB1 CC1


UPDATE employeeTable
 SET firstname = CASE When(employeeTable.firstName='N' and ID=10) THEN ValueTable.NewValue Else employeeTable.firstName End,
       lastname = CASE When(employeeTable.lastname='N' and ID=20) THEN ValueTable.NewValue Else employeeTable.lastname End,
       address = CASE When(employeeTable.address ='N' and ID=30) THEN ValueTable.NewValue Else employeeTable.address End
FROM ValueTable
where employeeTable.Empid=ValueTable.Empid

Any pointer will be great help

Cheers :)

vinod
__________________
Cheers :)

vinod
 
Old July 3rd, 2008, 04:52 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I *believe* you simply need to do something like this:

Code:
UPDATE employeeTable
SET firstname =  CASE When(employeeTable.firstName='N' and ID=10) THEN ValueTable.NewValue   Else employeeTable.firstName End,
    lastname =  CASE When(employeeTable.lastname='N' and ID=20) THEN ValueTable.NewValue  Else employeeTable.lastname End,
    address =  CASE When(employeeTable.address ='N' and ID=30) THEN  ValueTable.NewValue Else employeeTable.address End
FROM  employeeTable, ValueTable
where employeeTable.Empid = ValueTable.Empid
That is, you just need to repeat the table name in the FROM clause.
 
Old July 3rd, 2008, 05:07 PM
Friend of Wrox
 
Join Date: Oct 2004
Location: delhi, delhi, India.
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hi Pedant!!
I tried it,it will not show correct result.


Cheers :)

vinod
 
Old July 3rd, 2008, 06:39 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Try it with an alias???

An alias for second use of the main table?

UPDATE employeeTable
SET firstname = CASE When(ET.firstName='N' and ID=10) THEN VT.NewValue Else ET.firstName End,
    lastname = CASE When(ET.lastname='N' and ID=20) THEN VT.NewValue Else ET.lastname End,
    address = CASE When(ET.address ='N' and ID=30) THEN VT.NewValue Else ET.address End
FROM employeeTable ET, ValueTable VT
where ET.Empid = VT.Empid

This comes from the MS docs for UPDATE. But they don't try something quite as complex as this.

I'm sure we could easily do this in a Stored Proc via a #temp table, but that just offends my sensibilities <grin/>.
 
Old July 3rd, 2008, 06:53 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Yeah, I just tried that and it worked.

Simplified to only name column in the employees table, but worked like a charm:
Code:
update dbo.emps
set name = (CASE WHEN E.name = 'N' AND V.id=10 
                      THEN V.val
                      ELSE E.name END)
from dbo.emps AS E, dbo.vals AS V
where E.empid = V.empid




Similar Threads
Thread Thread Starter Forum Replies Last Post
Query with case marcomfigueroa Oracle 7 January 22nd, 2013 03:01 AM
update multiple column based on case statement?? vinod_yadav1919 SQL Server 2000 6 July 3rd, 2008 07:29 PM
update with case darkhalf Access 3 February 20th, 2008 12:00 AM
Using CASE in SUM column tnzabo Oracle 1 March 26th, 2007 10:29 AM
UPDATE trigger won't fire on CASE-only condition _Katka_ SQL Server 2000 1 November 14th, 2003 11:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.