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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Cursor

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 YTDTaxbl
101 33 $100.00
101 40 $70.00
101 60 $50.00

If employee number 101 has $100.00 YTDTaxbl for taxcode 33
and $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 YTDTaxbl
101 33 $100.00
101 40 $70.00
101 60 $100.00

This is the code I used:

SET NOCOUNT ON
DECLARE @empno smallint
DECLARE ecursor CURSOR FOR
select distinct empno
from taxtable
where taxcode in (33,60)


OPEN ecursor
FETCH NEXT FROM ecursor
INTO @empno
WHILE @@FETCH_STATUS = 0

BEGIN
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 @empno
END

CLOSE ecursor
DEALLOCATE ecursor
GO



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 taxtable
set 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.
Go to Top of Page

rkc01
Starting Member

43 Posts

Posted - 2003-02-03 : 14:40:24
Thanks nr. Beautiful in it's simplicity.

-Rob

Go to Top of Page
   

- Advertisement -