Results 1 to 7 of 7

Thread: sql server while loop...

  1. #1

    Thread Starter
    Hyperactive Member binilmb's Avatar
    Join Date
    Nov 2005
    Location
    Kochi
    Posts
    472

    sql server while loop...

    how to use while loop to fetch record in sql server without using cursor...
    ßįňįl

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: sql server while loop...

    Can you explain a little more of what your trying to do?

    Create a progressbar to display the recordset population during a long rs.open method call?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: sql server while loop...

    I think that using a Cursor is the only way to Fetch records in SQl Server, but maybe the question should be.. do you really need to Fetch records? What are you trying to do?

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: sql server while loop...

    I thought about the terminology used too as Fetch is a cursor method call.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Hyperactive Member binilmb's Avatar
    Join Date
    Nov 2005
    Location
    Kochi
    Posts
    472

    Re: sql server while loop...

    i have storeprocedure in which
    i need to open a table and retrive some records and update some values in it...
    using cursor im able to do so...
    but in many sites it is mentioned that using cursor will use more system resources...
    so is there any other possibilities fetching records without using cursor...
    ßįňįl

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: sql server while loop...

    Use an Update query.

    Update TableName
    Set Field1 = Value, Field2 = Value2...
    Where....

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: sql server while loop...

    As BRUCEVDE said - use an UPDATE query. Almost everything can be done in a "single" set-based logic update...

    But if you cannot do it in an UPDATE, another way is to loop through a table and process rows - I do this in STORED PROCEDURES all the time

    btw - this should have been posted in the DATABASE DEVELOPMENT section of this forum...

    Now assuming that your "table" has an integer primary key and you are going to process them in order from first through last...

    Code:
    Declare @PriKey Int
    
    Set @PriKey=(Select Min(ColumnA) From SomeTable)
    
    While @PriKey is not null
    Begin
       .
       .
       ... process your row here...
       .
       .
       Set @PriKey=(Select Min(ColumnA) From SomeTable Where ColumnA>@PriKey)
    End
    This code will loop through rows in a table - one at a time. It will do this without incurring CURSOR-type memory issues...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width