Assume that I am using the below table and need to fetch one by one all rows and do some operation on each row data.
Table structure
  1. CREATE TABLE [dbo].[User_Login](
  2. [userid] [int] IDENTITY(1,1) NOT NULL,
  3. [Username] [varchar](100) NULL,
  4. [User_Pass] [varchar](100) NULL,
  5. [F_Name] [varchar](20) NULL,
  6. [L_Name] [varchar](20) NULL,
  7. [Address1] [varchar](200) NULL,
  8. [mobile] [varchar](20) NULL,
  9. [City] [varchar](20) NULL
  10. )
We can use a while loop, get the column data and do some operations.
  1. declare @cnt int
  2. declare @UnitCount int
  3. set @cnt=1
  4. set @UnitCount=(select count(*) from User_Login)
  5. while @cnt<@UnitCount
  6. begin
  7. select top(1)* from User_Login where userid not in(select top(@cnt-1) userid from User_Login) order by userid
  8. --Do your operation like fetch data into variables and update it into another table etc..
  9. set @cnt=@cnt+1
  10. end