Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rkc01
Starting Member
43 Posts |
Posted - 2003-02-02 : 19:26:06
|
I know cursors have been getting alot of attention here lately but this is an area where I have a hard time. I just think in terms of 1 result at a time, (as opposed to result sets). Here's the situation:3 columns - empno, taxcode, YTDTaxbl (PK on empno, taxcode)An employee can have multiple taxcodes, (33, 40, 60, etc...)I want to take the higher value between tax code 33 and 60 and apply that value to both. Example:empno taxcode YTDTaxbl101 33 $100.00101 40 $70.00101 60 $50.00If employee number 101 has $100.00 YTDTaxbl for taxcode 33and $50.00 for YTDTaxbl for taxcode 60, change the YTDTaxbl to $100.00 for taxcode 60. And vise versa if the higher YTDTaxbl amount is in taxcode 60. So then it would look like:empno taxcode YTDTaxbl101 33 $100.00101 40 $70.00101 60 $100.00This is the code I used:SET NOCOUNT ONDECLARE @empno smallintDECLARE ecursor CURSOR FOR select distinct empno from taxtable where taxcode in (33,60)OPEN ecursorFETCH NEXT FROM ecursor INTO @empnoWHILE @@FETCH_STATUS = 0BEGIN update taxtable set ytdtxbl = (select max(ytdtxbl) from taxtable where taxcode in (33,60) and empno = @empno) where taxcode in(33,60) and empno = @empno FETCH NEXT FROM ecursor INTO @empnoENDCLOSE ecursorDEALLOCATE ecursorGO How could I do the update on each employ without using the cursor?Thx-Rob |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-02-02 : 20:12:30
|
| update taxtableset ytdtxbl = (select max(t2.ytdtxbl) from taxtable t2 where t2.taxcode in (33,60) and t2.empno = taxtable.empno)where taxcode in(33,60)==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
rkc01
Starting Member
43 Posts |
Posted - 2003-02-03 : 14:40:24
|
| Thanks nr. Beautiful in it's simplicity.-Rob |
 |
|
|
|
|
|
|
|