Thursday, January 10, 2008

SQl, T-SQL, Avoid using cursor, alternative of cursor

Avoid using cursor, if primary key is on the working table, in some cases, we can achieve cursor functionality with following

declare @currid int
select @currid = min(OrderID) from Orders where OrderDate < ''7/10/1996' while @currid is not null begin select @currid = min(OrderID) from Orders where OrderDate < '7/10/1996' and OrderID > @currid
end

May be helpful in some cases..

No comments: