Here is an example I stole from stackoverflow (http://stackoverflow.com/questions/24168/why-are-relational-set-based-queries-better-than-cursors):--CursorDECLARE @phoneNumber char(7)DECLARE c CURSOR LOCAL FAST_FORWARD FOR SELECT PhoneNumber FROM Customer WHERE AreaCode IS NULLOPEN cFETCH NEXT FROM c INTO @phoneNumberWHILE @@FETCH_STATUS = 0 BEGIN DECLARE @exchange char(3), @areaCode char(3) SELECT @exchange = LEFT(@phoneNumber, 3) SELECT @areaCode = AreaCode FROM AreaCode_Exchange WHERE Exchange = @exchange IF @areaCode IS NOT NULL BEGIN UPDATE Customer SET AreaCode = @areaCode WHERE CURRENT OF c END FETCH NEXT FROM c INTO @phoneNumberENDCLOSE cDEALLOCATE cEND--SetUPDATE Customer SET AreaCode = AreaCode_Exchange.AreaCodeFROM CustomerJOIN AreaCode_Exchange ON LEFT(Customer.PhoneNumber, 3) = AreaCode_Exchange.ExchangeWHERE Customer.AreaCode IS NULL
- LumbagoMy blog (yes, I have a blog now! just not that much content yet) -> www.thefirstsql.com