Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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
Reply With Quote
  #2 (permalink)  
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.
Reply With Quote
  #3 (permalink)  
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
Reply With Quote
  #4 (permalink)  
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/>.
Reply With Quote
  #5 (permalink)  
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 05:14 AM.


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