Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2004
    Location
    Bangalore, India
    Posts
    250

    Unanswered: Looping in Stored Procedures

    can we write loops in MSSql stored procedures ?

    my aim is to do something like

    select * from table
    while < recordset is not blank >
    do something...
    next record
    end while

    is this possible in stored procedure or should i do this from my client
    application ?

    pl explaint
    Cheers....

    baburajv

  2. #2
    Join Date
    Jan 2003
    Location
    India
    Posts
    523
    u can very well do this.
    for eg:
    create procedure looping
    as
    begin
    declare @l int
    set @l=10
    while @l>1
    begin
    print @l
    set @l=@l-1
    end
    end

    or it can be a condition like
    while exists (select * from table)
    begin
    some code....
    end

  3. #3
    Join Date
    Feb 2004
    Location
    Bangalore, India
    Posts
    250

    Loops In Sql Server

    thanks for the information

    but have one more qn

    im SELECTing some rows from a table
    and in the loop, i want to perform some operation on each selcted row
    after performing the operations , will the loop moves to the next
    record ?


    pl comment
    Cheers....

    baburajv

  4. #4
    Join Date
    Jan 2003
    Location
    India
    Posts
    523
    no.
    for that u need to use cursors.
    but use of cursors is not the recomended way of doing things in sql.
    so if u could post what u r trying to do, some one out here will surely be help u out.

Posting Permissions

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