 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 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
|
|
|
|

July 3rd, 2008, 03:21 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
|
|
update multiple column based on case statement??
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
__________________
Cheers :)
vinod
|
|

July 3rd, 2008, 04:54 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
*** CROSSPOST ***
Answered in Database / SQL Language forum.
Don't these Wrox forums discourage cross-posting???? If not, they should.
|
|

July 3rd, 2008, 04:58 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Are you using SQL Server 2005? If so you can first turn the value table into something easier to manage using the PIVOT operator. (If using an earlier version then you can still create the cross tab table but it's messier.)
Code:
SELECT EmpId, [10] FirstName, [20] LastName, [30] Address
FROM
(SELECT EmpId, NewValue, Id FROM ValueTable) VT
PIVOT
(
MAX(NewValue) FOR Id IN ([10], [20], [30])
) PVT
This enables the UPDATE to be written as:
Code:
UPDATE EmployeeTable
SET
FirstName = CASE ET.FirstName WHEN 'N' THEN UT.FirstName ELSE ET.FirstName END,
LastName = CASE ET.LastName WHEN 'N' THEN UT.LastName ELSE ET.LastName END,
Address = CASE ET.Address WHEN 'N' THEN UT.Address ELSE ET.Address END
FROM
EmployeeTable ET INNER JOIN
(SELECT EmpId, [10] FirstName, [20] LastName, [30] Address
FROM
(SELECT EmpId, NewValue, Id FROM ValueTable) VT
PIVOT
(
MAX(NewValue) FOR Id IN ([10], [20], [30])
) PVT) UT
ON ET.EmpId = UT.EmpId;
--
Joe ( Microsoft MVP - XML)
|
|

July 3rd, 2008, 05:22 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Quote:
quote:Originally posted by Old Pedant
*** CROSSPOST ***
Answered in Database / SQL Language forum.
Don't these Wrox forums discourage cross-posting???? If not, they should.
|
They certainly do but remember the mantra, rules only apply to others :)
--
Joe ( Microsoft MVP - XML)
|
|

July 3rd, 2008, 06:18 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hi Joe,
Thank you for providing the inputs, I am using SQL2000.
My sincere aplogoies for cross post.
If you have any example for doing this in SQL2000 it would be a great help!!
Cheers :)
vinod
|
|

July 3rd, 2008, 06:45 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hi All,
Now I am able to update in SQL2000..
Thanks again all of you to give the pointer on it
Cheers :)
vinod
|
|

July 3rd, 2008, 07:29 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
So what did you end up using???
You should post your working answer so others who might stumble upon this will not have to ask again.
|
|
 |