Friday, September 16, 2011

While Loop : An SQL Server 2005 alternative for cursors


While Loop in SQL Server

SQL Server 2005 alternative for cursors is While loop. SQL Server 2005 provides a while loop which we can use to iterate through any table having at least one column. The column must have unique value (primary key is generally prefered). A sample code is provided below which is MSSQL Server Specific code and is at least 5 times faster than cursor.
Declare @PresentIDint;
Set @PresentID = (Select min(column_name) from your_table );
/*Starts the iterrational loop*/
While @PresentID is not null
Begin
/*Code segment where you have access to @PresentID*/
/*Next line required to keep iterating*/
Set @PresentID = (Select min(column_name) from your_table where column_name > @PresentID);
End


Relevant AD
This ad may be helpful, if relevant to your search.



Summary

Cursor is an SQL Server 2000 feature similar to the iteration loop available in the several programming languages.  The nearest match for Cursor is While loop and For Loop in the programming languages. Cursor should be avoided as much as possible.
[Did you like this post ? Do leave a comment or share with your Facebook friends. This will give me more reasons to write such posts.]

21 comments:

  1. toms outlet, http://www.tomsoutlet-stores.com/
    nike roshe, http://www.nikerosherunshoes.co.uk/
    prada handbags, http://www.pradahandbagsoutlet.co.uk/
    soccer jerseys, http://www.cheapsoccerjersey.net/
    michael kors outlet store, http://www.michaelkorsoutlet-store.us.com/
    true religion jeans, http://www.truereligionjeansoutlets.us.com/
    oakley sunglasses, http://www.oakleysunglasses-outlet.us.com/
    nike free 5, http://www.nikefree5.us/
    louis vuitton bags, http://www.louisvuittonbag.us.com/
    chanel handbags, http://www.chanelhandbags-outlet.co.uk/
    chanel handbags, http://www.chanelhandbags-outlet.us.com/
    louis vuitton handbags, http://www.louisvuittonhandbag.us/
    louis vuitton outlet, http://www.louisvuittonoutlet.in.net/
    ghd hair straighteners, http://www.ghdhairstraightenerssale.co.uk/
    nba jerseys, http://www.nbajerseys.us.com/
    coach outlet, http://www.coachoutletstores.com.co/
    true religion jeans, http://www.truereligionjeanscanada.com/
    swarovski crystal, http://www.swarovskicrystals.co.uk/
    mbt shoes, http://www.mbtshoesoutlet.us.com/
    michael kors handbags, http://www.michaelkorshandbags.in.net/
    chanel handbags, http://www.chanelhandbagsoutlet.in.net/
    michael kors outlet online, http://www.michaelkorsoutletonline.in.net/
    polo ralph lauren, http://www.poloralphlauren.us.org/
    the north face clearance, http://www.thenorthfaceclearances.us.com/
    jordan shoes, http://www.jordan-shoes.us.com/
    beats by dr dre, http://www.beatsbydrdre-headphones.us.com/
    the north face uk, http://www.thenorthfaces.org.uk/
    0929maoqiuyun

    ReplyDelete
  2. steelers jerseys, http://www.pittsburghsteelersjersey.com/
    michael kors handbags, http://www.michaelkorsusa.us/
    cheap ugg boots, http://www.uggaustralia.in.net/
    kobe bryants shoes, http://www.kobeshoes.us/
    louis vuitton handbags outlet, http://www.louisvuittonhandbag.us/
    true religion outlet, http://www.truereligionjeansoutlet.com/
    cheap nba jerseys, http://www.nbajerseys.us.com/
    san antonio spurs, http://www.sanantoniospursjersey.com/
    tiffany and co, http://www.tiffanyandco.in.net/
    wellensteyn outlet, http://www.barbour.us.com/
    adidas outlet, http://www.adidasoutletstore.us.com/
    canada goose outlet, http://www.canadagooseoutlet.com.co/
    michael kors outlet, http://www.michaelkorsoutlet-store.us.com/
    bottega veneta outlet, http://www.bottegaveneta-outlet.net/
    canada goose outelt, http://www.uggbootscheap.eu.com/
    hollister shirts, http://www.hollistershirts.co.uk/
    tiffany and co, http://www.tiffany-outlet.us.com/
    swarovski outlet, http://www.swarovski-outlet.co.uk/
    basketball shoes,basketball sneakers,lebron james shoes,sports shoes,kobe bryant shoes,kobe sneakers,nike basketball shoes,running shoes,mens sport shoes,nike shoes
    winter jackets, http://www.cybermonday2015.us.com/
    mm1017

    ReplyDelete
  3. Kendall Jenner’s the model of the moment. Having walked for the likes of Chanel, Dolce and Gabbana, Fendi, Marc Jacobs and Givenchy she is racking up a cheap ray ban sunglasses sale under 30$ serious CV. Victoria Beckham is said to have earmarked her for her next catwalk show. Having met with her recently the designer is cheap ray bans said to have called her the most naturally beautiful model.All that catwalk dressing is giving her some great styling sense. At Geoffrey’s in ray ban outlet Malibu she’s out celebrating her dad’s birthday with sister Kylie and half sisters Kim, Khloe and Kourtney. Like many a model that has Ray Ban Sunglassses Sale access to top designer sunglasses from the big fashion houses Ray-Ban is still a go-to. She coordinates her look with the Ray-Ban 3025 cheap ray bans for sale Aviator with gold frame and blue lens. The gold frame is synced up with her sandals, gold bag hardware, watch and ring. The ray ban sunglasses pale blue gradient lens is the perfect compliment to that pale pink jump suit. The aviator shape does a good job of toughening ray ban store up that pink too.
    That the peaks of red carpet dressing leave A list ladies hankering for high glamour off duty is ray ban aviators no surprise. Fine designer sunglasses are one the first ports of call. Actress Amy Adams is currently the face for MaxMara. But she’s cheap ray ban sunglasses forgoing the house for another Italian legend when it comes to sunglasses though here.Shopping with the family in Los Angeles she’s wearing Bulgari Ray Ban Sunglassses

    ReplyDelete