Thursday, November 23, 2006

SQL: Using while loop for replacing cursor

Following SQL code snippet shows how we can use while loop :


create table #t (pk_counter int identity(1,1), pk_subquestion int, fk_question int, subquestion_text varchar (500), subquestion_type varchar (50), subquestion_order varchar(50), required_flag bit)

declare @counter int
declare @maxcount int
declare @subquestionid int
select @maxcount = count(pk_counter) from #t
set @counter = 1


while (@counter < @maxcount + 1) begin select @subquestionid=pk_subquestion from #t where pk_counter=@counter exec stp_get_options @subquestionid set @counter = @counter + 1 end
drop table #t

No comments: