Iterate through rows in SQL Server 2008

后端 未结 3 2185
渐次进展
渐次进展 2020-12-14 09:07

Consider the table SAMPLE:

id       integer
name     nvarchar(10)

There is a stored proc called myproc. It takes only one para

3条回答
  •  感情败类
    2020-12-14 09:19

    I just declare the temporary table @sample and insert the all rows which have the name='rahul' and also take the status column to check that the row is iterated.and using while loop i iterate through the all rows of temporary table @sample which have all the ids of name='rahul'

    use dumme
    
    Declare @Name nvarchar(50)
    set @Name='Rahul'
    DECLARE @sample table (
    
        ID int,
        Status varchar(500)
    
        )
    insert into @sample (ID,status) select ID,0 from sample where sample=@name
    while ((select count(Id) from @sample where status=0 )>0) 
    begin
        select top 1  Id  from @sample where status=0 order by Id
        update @sample set status=1  where Id=(select top 1  Id  from @sample where status=0 order by Id) 
    end
    

提交回复
热议问题